开发者

Mysql fetch array error

开发者 https://www.devze.com 2023-01-12 19:15 出处:网络
Im getting an error on my site that appeared for the first time today, despite functioning fine for months.

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 开发者_StackOverflow中文版for your awesome answers in advance!


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());
}


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());
}
0

精彩评论

暂无评论...
验证码 换一张
取 消