Invalid MySQL result resource

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
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

Invalid MySQL result resource

Post by neridaj »

Hello,

I'm getting this error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in output_fns.php on line 416

Here are the two functions involved:

Code: Select all

function get_user_info($username)
{
    // connect to db
    $conn = db_connect();
 
    // query user info 
    $result = $conn->query("select first_name, last_name, email from user where username='$username'"); 
    if (!$result)
        return false;
    else
        return $result;
}
 
function display_folders()
{
    $userinforesult = get_user_info($username);
    if(!$userinforesult)
        echo 'nothing';
    else
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) { // line 416
            printf("Last Name: %s  Last Name: %s Email: %s", $row[0], $row[1], $row[2]);  
        }
    echo '<a href="logout.php">Logout</a>'; 
    $userfolder =  $_SESSION['valid_user'] . '/';
    echo '<table align="center" border="1" cellpadding="5"><tr><th></th></tr><tr>';
    $dir    = 'members/' . $userfolder;
    $files = scandir($dir);
    
    foreach($files as $value) {
        if(!in_array($value, array('.', '..'))) {
    
            // check for folders
            if(is_dir($dir.DIRECTORY_SEPARATOR.$value)) {
                printf('<td><a href="preview.php?pa=%s">'.
                    '<img src="'. $dir . $value .'.jpg" width=75" height="75" />'.
                    '<br />%s<a/></td>',
                    $value, $value);
            }
        }
    }
    echo '</tr></table>';
}
 
Is it because I'm not using mysql_query? I tried that and I just got connection errors so I reverted back to:
$result = $conn->query(), which at least connects and returns something. Is there an alternative to mysql_fetch_array() that I should be using instead?

Thanks for any help,

Jason
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Invalid MySQL result resource

Post by John Cartwright »

$result is initialized in get_user_info(), but display_folders() which uses the variable is never been given access. Take a look at variable scope if you don't know about scope.

A simple solution to your problem is have the get_user_info() return the resource instead of a boolean.

Code: Select all

function get_user_info($username)
{
    // connect to db
    $conn = db_connect();
 
    // query user info
    return $conn->query("select first_name, last_name, email from user where username='$username'");
}
 
function display_folders()
{
    $userinforesult = get_user_info($username);
    if(!$userinforesult)
        echo 'nothing';
    else
        while ($row = mysql_fetch_array($userinforesult, MYSQL_NUM)) { // line 416
            printf("Last Name: %s  Last Name: %s Email: %s", $row[0], $row[1], $row[2]);  
        }
 
    ....
 
 
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

Re: Invalid MySQL result resource

Post by neridaj »

I thought I was returning it:

Code: Select all

 
function get_user_info($username)
{
    // connect to db
    $conn = db_connect();
 
    // query user info 
    $result = $conn->query("select first_name, last_name, email from user where username='$username'"); 
    if (!$result)
        return false;
    else
        return $result;
}
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Invalid MySQL result resource

Post by markusn00b »

I'm an idiot
Last edited by markusn00b on Sun Apr 20, 2008 2:35 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Invalid MySQL result resource

Post by John Cartwright »

Oh sorry I thought you were only returning booleans. It sounds like your query is failing, does mysql_error() have anything to say?
Post Reply