Jump to content

Resource id #6 error when querying MySQL via PHP


MarkMan5

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]
Link to comment
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
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.