在PHP中使用mysql_fetch_assoc时发出警告 [英] Warning when using mysql_fetch_assoc in PHP

查看:73
本文介绍了在PHP中使用mysql_fetch_assoc时发出警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
警告:mysql_fetch_array():提供的参数不是有效的MySQL结果

Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

当我运行php页面时,出现此错误,并且不知道出了什么问题,有人可以帮忙吗?如果有人需要更多信息,我将发布整个代码.

When I run my php page, I get this error and do not know what's wrong, can anyone help? If anyone needs more infomation, I'll post the whole code.

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in
H:\Program Files\EasyPHP 2.0b1\www\test\info.php on line 16

<?PHP

    $user_name = "root";
    $password = "";
    $database = "addressbook";
    $server = "127.0.0.1";

$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

    $SQL = "SELECT * FROM tb_address_book";
    $result = mysql_query($SQL);

    while ($db_field = mysql_fetch_assoc($result)) {
        print $db_field['ID'] . "<BR>";
        print $db_field['First_Name'] . "<BR>";
        print $db_field['Surname'] . "<BR>";
        print $db_field['Address'] . "<BR>";
    }    

    mysql_close($db_handle);

}
else {
    print "Database NOT Found ";
    mysql_close($db_handle);
}

?>

推荐答案

通常表示您的SQL错误.

It generally means that you've got an error in your SQL.

$sql = "SELECT * FROM myTable"; // table name only do not add tb
$result = mysql_query($sql);
var_dump($result);    // bool(false)

很明显,false不是MySQL资源,因此会出现该错误.

Obviously, false is not a MySQL resource, hence you get that error.

编辑立即粘贴的代码:

while循环之前的行上,添加以下内容:

On the line before your while loop, add this:

if (!$result) {
    echo "Error. " . mysql_error();
} else {
    while ( ... ) {
       ...
    }
}

确保tb_address_book表确实存在,并且您已正确连接到数据库.

Make sure that the tb_address_book table actually exists and that you've connected to the DB properly.

这篇关于在PHP中使用mysql_fetch_assoc时发出警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆