Jump to content

Archived

This topic is now archived and is closed to further replies.

MarkMan5

Resource id #6 error when querying MySQL via PHP

Recommended Posts

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.

[code]
    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;
    }
[/code]

Share this post


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

Ken

Share this post


Link to post
Share on other sites
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.

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.