Forum Moderators: coopster

Message Too Old, No Replies

DB: from table where id = session['id'] HELP

         

dkin

1:29 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



This is my code.

$results = mysql_query("SELECT * FROM users where username = $_SESSION[username]",$link) or die ("query 1: " . mysql_error());
$myrow = mysql_fetch_array($results) or die ("query 2: " . mysql_error());

I think it is pretty easy to see what I am trying to do but all I am getting is this

"query 1: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"

please help

cheers

dkin

2:30 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



does anyone know, this has been holding me back all night lol.

dreamcatcher

3:47 pm on Jan 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi dkin,

You query needs to have apostrophes:

SELECT * FROM users where username = '$_SESSION[username]'

That should help I think.

dc

dkin

9:51 pm on Jan 4, 2005 (gmt 0)

10+ Year Member



when I do that I get an error with this line

$myrow = mysql_fetch_array($results) or die ("query 2: " . mysql_error());

But it is not displaying the error, it simply says query 2

Any ideas?

dreamcatcher

11:41 pm on Jan 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe the $link variable in your query is the problem?

Try just:

$results = mysql_query("SELECT * FROM users where username = '" . $_SESSION['username'] . "'") or die ("query 1: " . mysql_error());
$myrow = mysql_fetch_array($results) or die ("query 2: " . mysql_error());

dkin

1:10 am on Jan 5, 2005 (gmt 0)

10+ Year Member



I put that in and it still says query 2.

coopster

11:46 am on Jan 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Then you are not returning any rows. The query is probably successful, but returning FALSE as mysql_fetch_array() [php.net] returns an array that corresponds to the fetched row, or FALSE if there are no more rows.

Try dumping the sql statement and running it on a command line to see if you get any data or at least to see what value is in the $_SESSION variable you are using.

nshack31

1:39 pm on Jan 5, 2005 (gmt 0)

10+ Year Member



would this work?...

first set a variable..
$user=$_SESSION[username];

then use that with this code..

$results = mysql_query("SELECT * FROM users where username = '$user'",$link)

im not sure, give it a try I often find that storing the session/cookie in a variable works

dreamcatcher

2:14 pm on Jan 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you aren`t returning any rows, its strange that you aren`t receiving an error to say that mysql_fetch_array is not a valid resource in blah blah. This is usually what happens when you try and use mysql_fetch_array on an empty results set.

mcibor

2:29 pm on Jan 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no error if you get no answer. So just skip the second die:

$results = mysql_query("SELECT * FROM users where username = '" . $_SESSION['username'] . "'") or die ("query 1: " . mysql_error());
if(mysql_num_rows($result) $myrow = mysql_fetch_array($results);

or just

if($myrow = mysql_fetch_array($results))

Best regards!