0

Im getting an error on my site that appeared for the first time today, despite functioning fine for months.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /web_directory/index.php on line 33

Here is the code from those lines.

<?  $sql = "SELECT 
            p.id as 'id',
            p.post_title as 'client',
            (SELECT `meta_value` FROM `wp_postmeta` WHERE `post_id` = p.id AND `meta_key` = 'thumb1') as 'thumb'
            FROM
            `wp_posts` p
            INNER JOIN `wp_postmeta` pm ON (p.id = pm.post_id)
            INNER JOIN `wp_term_relationships` wtr ON (p.id = wtr.object_id)
            INNER JOIN `wp_term_taxonomy` tt ON (wtr.term_taxonomy_id = tt.term_taxonomy_id)
            WHERE 
            tt.term_id = 439
            AND tt.taxonomy = 'category'
            AND p.post_status = 'publish'
            GROUP BY p.id
            ORDER BY p.id DESC
            LIMIT 5
            ";
    $sql = mysql_query($sql);
    $i=0;
    while($s = mysql_fetch_array($sql))
    {
        $i++;
?> 

Im not the original developer of the site, and I dont have much of a knowledge with mysql. Thanks for your awesome answers in advance!

3
  • The majority of the WHILE loop is missing... Commented Aug 21, 2010 at 23:07
  • You should really consider using PDO or mysqli instead of the mysql extension. Commented Aug 21, 2010 at 23:27
  • I'm guessing that line 33 is while($s = mysql_fetch_array($sql))? Could you fill in the rest of the while loop? Commented Aug 21, 2010 at 23:27

2 Answers 2

1

There might be some error in your query; try adding or die(mysql_error()) to mysql_query like this:

$sql = mysql_query($sql) or die(mysql_error());

The mysql_query should return a resource identifier which you can check with:

if (!is_resource($sql)){
  die(mysql_error());
}
Sign up to request clarification or add additional context in comments.

Comments

0

Obviously you are feeding mysql_fetch_array() not a valid MySQL result resource.

This might happen whenever mysql_query fails for any reason. (query is wrong, database has gone away, network is down, etc etc.)

You should check if a call to mysql_query is successfull before trying to iterate over its data. If you add a check and print out what the error really is, you might pin it down yourself.

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

http://php.net/manual/en/function.mysql-query.php

Adding this before mysql_fetch_array() will make sure the script dies whenever an error has occurred including the actual error message:

$res = mysql_query($sql);
if (!$res) {
    die('Invalid query: ' . mysql_error());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.