开发者

Mysql+PHP problem

开发者 https://www.devze.com 2023-03-10 06:27 出处:网络
This is my \"person\" table: eid sex email 17Femaleexample@example.com This is开发者_Python百科 my php page:

This is my "person" table:

eid sex email   
17  Female  example@example.com

This is开发者_Python百科 my php page:

<?php
$loginquery = mysql_query('SELECT eid, email, sex FROM person WHERE email = "'example@example.com'" ;') or die("Query fail: " . mysql_error() );
$userlogin = mysql_fetch_array($loginquery);

if($userlogin){
$_SESSION['eid']= $userlogin['eid'];
$_SESSION['email']= $userlogin['email'];
$_SESSION['sex']= $userlogin['sex'];
}


echo "user ".$_SESSION['eid']." ".$_SESSION['email']." ".$_SESSION['sex']; 
?>

This is the output:

user example@example.com Female

Why the $_SESSION['eid'] is not printed ?


I see at least 2 bugs

session_start(); is not called and it should.

The query is weird:

$loginquery = mysql_query('SELECT eid, email, sex FROM person WHERE email = "'example@example.com'" ;') or die("Query fail: " . mysql_error() );

I don't know why this does not produce an error, but your string is not build well. Try something like

$loginquery = mysql_query("SELECT eid, email, sex FROM person WHERE email = 'example@example.com';") or die("Query fail: " . mysql_error() );


i tried this and it works:
sqlErr() is my own function for error handling

$dbcon=@mysql_connect("localhost","root","") or sqlErr();
@mysql_select_db("test",$dbcon) or sqlErr();

$loginquery = mysql_query("SELECT * FROM person WHERE email = 'example@example.com' ;") or die("Query fail: " . mysql_error() );
$userlogin = mysql_fetch_array($loginquery,MYSQL_ASSOC);

if($userlogin){
$_SESSION['eid']= $userlogin['eid'];
$_SESSION['email']= $userlogin['email'];
$_SESSION['sex']= $userlogin['sex'];
}


echo "user ".$_SESSION['eid']." ".$_SESSION['email']." ".$_SESSION['sex'];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号