...

View Full Version : can i do this?



mivec
04-12-2004, 11:52 AM
hi all,
hello, u see, i am trying to check the username with the session...like this



$sql = "SELECT * from tblgallery username = " .$_SESSION["userID"];
$rs = mysql_query($sql);
$nRows = mysql_num_rows($rs);


but i am getting this notice :



Notice: Undefined variable: _SESSION in c:\program files\apache group\apache\htdocs\ewedding\photoupload.php on line 199



coz i will be needing the username to insert into the database. so can i insert it like this?



"INSERT INTO tblgallery (username) VALUES ('" . $_SESSION["userID"] . "');


i am not too sure about sessions...pls some one help...thank you so much

raf
04-12-2004, 02:18 PM
Please choose a meaningfull title.

About the message --> it's a notice, not an error. Run a search for errorreporting for more info and possible fixes.
About your actual task, is 'username' a string or numerical varaible ?
$sql = "SELECT * from tblgallery username = " .$_SESSION['userID'];
is just fine if it's a numerical column, else it's
$sql = "SELECT * from tblgallery username = '" .$_SESSION['userID'] . "'";

jeskel
04-12-2004, 05:07 PM
hi all,
hello, u see, i am trying to check the username with the session...like this



$sql = "SELECT * from tblgallery username = " .$_SESSION["userID"];
$rs = mysql_query($sql);
$nRows = mysql_num_rows($rs);


but i am getting this notice :

A notice usually tells you that a variable is undefined bwfore you use it. So make sure that the variable in question is defined. PHP is set to default to show notices even tho many hosts disable this feature.

dniwebdesign
04-12-2004, 05:33 PM
It would be easier to do something like this...


$user=$_SESSION["userID"]

$sql = "SELECT * from tblgallery username='.$user.'"
$rs = mysql_query($sql);
$nRows = mysql_num_rows($rs);


and your insert method would be:


"INSERT INTO tblgallery (username) VALUES ('$user')";



Also as raf said make sure that there is something in the session variable...

raf
04-12-2004, 06:07 PM
It would be easier to do something like this...


$user=$_SESSION["userID"]

$sql = "SELECT * from tblgallery username='.$user.'"
$rs = mysql_query($sql);
$nRows = mysql_num_rows($rs);


No. Your $sql line would throw 2 error.

it's either


$sql = "SELECT * from tblgallery WHERE username='$user'";
//or
$sql = "SELECT * from tblgallery WHERE username='" . $user . "'";
//or
$sql = 'SELECT * from tblgallery WHERE username=\''.$user.'\'';


I prefere the second one because it's better readable and i always concatenate strings and variables (i never just include the variables). The first version would be my second choice.
The third one will probably be executed slightly faster (no unnescecary parsing no the sql-statement) but it's harder to read and not as portable (if you need to testrun it somewhere).
sql-statements and eval'd code are about the only strings where i use double quotes.

dniwebdesign
04-12-2004, 06:32 PM
oh, dang... forgot the double quotes...

sidney
04-12-2004, 10:06 PM
am i mistaken

dont you need a WHERE clause


$sql = "SELECT * from tblgallery WHERE username='" . $user . "'";

raf
04-13-2004, 12:36 AM
indeed.

dniwebdesign
04-13-2004, 01:06 AM
yea, I was thinking that too but I a$sumed Raf knew what he was doing and he didn't use it.... so much for assuming..

raf
04-13-2004, 01:15 AM
yea, I was thinking that too but I a$sumed Raf knew what he was doing and he didn't use it.... so much for assuming..i didn't realy read it. just copy and pased it :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum