Resource ID #6 issues

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Resource ID #6 issues

Post by tristanlee85 »

Here is my code:

Code: Select all

$getmanager="SELECT name FROM members WHERE user = '$fedex_username'";
$manager=mysql_query($getmanager);
When $manager is called, "Resource ID #6" is echo'd. I tried mysql_result() and that didn't work either. What's going on?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

mysql_query returns the resource id of the query. You need to fetch the array using mysql_fetch_array() in order to get the data out.

Code: Select all

<?php
$query = "SELECT name FROM members WHERE user = '$fedex_username'";
$result = mysql_query($result) or die();
while ($row = mysql_fetch_array($result)) {
    $my_array = $row;
}
?>
Now access your data from within the array $my_array.

PS You could also echo your data from with the while loop also. I like to set the data into arrays for use later. This is just personal preference.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Well, I did this and it worked:

Code: Select all

$getmanager="SELECT name FROM members WHERE user = '$fedex_username'";
$manager=mysql_query($getmanager);
$manager_name = mysql_fetch_row($manager);
And then later on down the page I echoed out the "name" like this:

Code: Select all

<? echo "value=\"$manager_name[0]\">";
It worked, but I'm not sure why it worked. I saw this example on another post. Why do I use the [0]?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

0 in that case happens to be the index of the array you are using. The array should be set up something like $array[int_index]['dbfieldname']. You can use a for loop to run through it or other looping procedure.
Post Reply