在PHP中使用mysql_fetch_assoc时出现警告[重复]

时间:2022-06-12 21:31:50

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

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

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.

当我运行我的php页面时,我收到此错误并且不知道什么是错的,有人可以帮忙吗?如果有人需要更多信息,我会发布整个代码。

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

?>

2 个解决方案

#1


11  

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

它通常意味着您的SQL中出现错误。

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

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

显然,false不是MySQL资源,因此您会收到该错误。

EDIT with the code pasted now:

编辑现在粘贴的代码:

On the line before your while loop, add this:

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

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

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

确保tb_address_book表实际存在并且您已正确连接到DB。

#2


0  

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

?>

#1


11  

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

它通常意味着您的SQL中出现错误。

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

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

显然,false不是MySQL资源,因此您会收到该错误。

EDIT with the code pasted now:

编辑现在粘贴的代码:

On the line before your while loop, add this:

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

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

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

确保tb_address_book表实际存在并且您已正确连接到DB。

#2


0  

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

?>