Jump to content


Photo

Resource id #6 error when querying MySQL via PHP


  • Please log in to reply
2 replies to this topic

#1 MarkMan5

MarkMan5
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 03 March 2006 - 09:04 AM

Below is my code snippet, I'm sure there is an easy fix but I am stumped. Any help is appreciated :-) -thanks!

It returns "Resource id #6" and I've tried tweaking the query many, many times with no luck.

    function grabData($column, $table, $index)
    {
    /* connect to db */
    mysql_connect('localhost','DBName,'PW');
    $dbcnx=mysql_select_db('TableName');
    if (!$dbcnx) {
    exit("Unable to connect to the database server at this time.");
    }
    /* construct and run the query */
    $query = "SELECT $column FROM $table WHERE ItemNumber = $index";
    $result=mysql_query($query);
    echo $result;
    return;
    }


#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 March 2006 - 01:24 PM

In these lines of code:
<?php
    $query = "SELECT $column FROM $table WHERE ItemNumber = $index";
    $result=mysql_query($query);
    echo $result;
?>
The variable $result now contains a resource (or a pointer to the results). You're not using the function to actually get the data.
<?php $row = mysql_fetch_assoc($result); ?>
Now the variable $row is an array containing all the fields, to see what it contains, you can do
<?php echo '<pre>'.print_r($row,true).'</pre>'; ?>

Ken

#3 MarkMan5

MarkMan5
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 03 March 2006 - 06:18 PM

Thanks Ken! That got me closer to where I wanted to be (printing just the one value)

I replaced your print_r statement with the code below, and it prints just the value. Please advise if there is a better or cleaner way to do this. Many thanks again.

/* print the result */
foreach ($row as $value){
print_r ($value);
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users