Page 1 of 1

help -- getting Resource id #X on mysql query!

Posted: Thu Oct 16, 2003 11:05 am
by ecourt
ok -- I'm getting really confused!!!

I'm running this query on a mysql database (I can run it from phpmysql, and it works, and returns exactly what it should)

$id = mysql_query("SELECT userid FROM user where username='eric'");
print "id is: $id<br>";

for some reason -- it prints out Resource id #4 on the screen, If I loop through several, the number keeps going up ...

What am I doing wrong!! :)

THANKS!!!

Posted: Thu Oct 16, 2003 11:13 am
by maniac9
are you trying to get the resource id of the session, or the actually value of the user id?

Posted: Thu Oct 16, 2003 11:15 am
by ecourt
the actual value that is in userid (a field in the user table)

Posted: Thu Oct 16, 2003 11:19 am
by qads

Code: Select all

<?php
$id = mysql_fetch_array(mysql_query("SELECT userid FROM user where username='eric'")); 
print "id is: ".$id[userid]."<br />"; 
?>
read this: viewtopic.php?p=62787&highlight=#62787

Posted: Thu Oct 16, 2003 11:21 am
by maniac9
ah

first of all, you can't get separate values using the mysql_query() function - that returns an entire result

you should use something like this

Code: Select all

$result = mysql_query("SELECT userid FROM user WHERE username='eric'");

while ($row = mysql_fetch_array($result))
{
    echo($row['userid'] . "<br />");
}
There are a few different function to use besides mysql_fetch_array, but with that one, you can call up each field by it's name

also, this function will loop through however many records are returned from the query. if there are 3 users with the username eric, then you'll have three results

Posted: Thu Oct 16, 2003 11:22 am
by ecourt
yea -- I just realised that ---

I was thinking that since I was only requesting 1 piece of data, I could slap it straight into a variable, and not have to do the mysql_fetch_*.

Thanks!

Posted: Thu Oct 16, 2003 11:33 am
by maniac9
np

Posted: Fri Oct 17, 2003 5:45 am
by twigletmac
If you only need one result you can also use mysql_result() which can be simpler than having to fetch the array and then get the value out.

Mac