'警告:mysql_fetch_array()期望参数1是资源,

时间:2014-03-23 15:27:46

标签: php mysql sql warnings

我的代码收到错误:

如何修复警告:*警告:mysql_fetch_array()期望参数1为资源,布线在第41行的C:\ xampp \ htdocs \ autocomplete \ ajax_query.php中给出*

<?php

header('Content-type: text/html; charset=UTF-8');

$hostname = 'localhost';
$username = 'root';
$password = '';
$dbname = 'quora';

mysql_connect( $hostname, $username, $password ) or die ( 'Erro ao tentar conectar ao banco de dados.' );
mysql_select_db( $dbname );

if( isset( $_REQUEST['query'] ) && $_REQUEST['query'] != "" )
{
    $q = mysql_real_escape_string( $_REQUEST['query'] );

    if( isset( $_REQUEST['identifier'] ) && $_REQUEST['identifier'] == "estado")
    {
    $sql = "SELECT * FROM category where locate('$q',name) > 0 order by locate('$q',name) limit 1";
    $r = mysql_query( $sql );
    if ( $r )
    {
        echo '<ul>'."\n";
        while( $l = mysql_fetch_array( $r ) )
        {
        $p = $l['name'];
        $p = preg_replace('/(' . $q . ')/i', '<span style="font-weight:bold;">$1</span>', $p);
        echo "\t".'<li id="autocomplete_'.$l['id'].'" rel="'.$l['id'].'_' . $l['name'] . '">'. utf8_encode( $p ) .'</li>'."\n";
        }
        echo '</ul>';
    }
    }

    if( isset( $_REQUEST['identifier'] ) && $_REQUEST['identifier'] == "estado")
    {
    $sql = "SELECT * FROM story where locate('$q',name) > 0 $sql order by locate('$q',name) limit 10";
    $r = mysql_query( $sql );
    if ( count( $r ) > 0 )
    {
        echo '<ul>'."\n";
        while( $l = mysql_fetch_array( $r ) )
        {
        $p = $l['name'];
        $p = preg_replace('/(' . $q . ')/i', '<span style="font-weight:bold;">$1</span>', $p);
        echo "\t".'<li id="autocomplete_'.$l['id'].'" rel="'.$l['id'].'_' . $l['name'] . '">'. utf8_encode( $p ) .'</li>'."\n";
        }
        echo '</ul>';
    }
    }
}

?>

感谢您的帮助 错误行在这里

 while( $l = mysql_fetch_array( $r ) )

2 个答案:

答案 0 :(得分:0)

您的查询无效。您不能在列名称周围使用单引号。

$sql = "SELECT * FROM category where locate('$q',name) > 0 order by locate('$q',name) limit 1";

应该是:

$sql = "SELECT * FROM category where locate($q,name) > 0 order by locate($q ,name) limit 1";

答案 1 :(得分:0)

你的做法有误。而不是mysql_fetch_array(),你应该使用mysql_fetch_row()。

像这样,

while( $l = mysql_fetch_row( $r ) )