Page 1 of 1

Invalid MySQL result resource

Posted: Sat Apr 19, 2008 12:45 pm
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

Re: Invalid MySQL result resource

Posted: Sat Apr 19, 2008 1:57 pm
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]);  
        }
 
    ....
 
 

Re: Invalid MySQL result resource

Posted: Sat Apr 19, 2008 5:26 pm
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;
}

Re: Invalid MySQL result resource

Posted: Sun Apr 20, 2008 4:19 am
by markusn00b
I'm an idiot

Re: Invalid MySQL result resource

Posted: Sun Apr 20, 2008 10:49 am
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?