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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
ecourt
Forum Newbie
Posts: 11
Joined: Mon Oct 13, 2003 9:53 pm

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

Post 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!!!
maniac9
Forum Commoner
Posts: 55
Joined: Fri Aug 01, 2003 12:27 am
Location: Arkansas, USA
Contact:

Post by maniac9 »

are you trying to get the resource id of the session, or the actually value of the user id?
ecourt
Forum Newbie
Posts: 11
Joined: Mon Oct 13, 2003 9:53 pm

Post by ecourt »

the actual value that is in userid (a field in the user table)
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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
Last edited by qads on Thu Oct 16, 2003 11:26 am, edited 1 time in total.
maniac9
Forum Commoner
Posts: 55
Joined: Fri Aug 01, 2003 12:27 am
Location: Arkansas, USA
Contact:

Post 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
ecourt
Forum Newbie
Posts: 11
Joined: Mon Oct 13, 2003 9:53 pm

Post 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!
maniac9
Forum Commoner
Posts: 55
Joined: Fri Aug 01, 2003 12:27 am
Location: Arkansas, USA
Contact:

Post by maniac9 »

np
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply