tags:

views:

438

answers:

3

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

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

?>
+11  A: 

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

$sql = "SLEECT * FROM myTable";
$result = mysql_query($sql);
var_dump($result);    // bool(false)

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

EDIT with the code pasted now:

On the line before your while loop, add this:

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.

nickf
Ok i found out the error was, and thanks for your guys help
Anonymous: it would be nice if you'd post your answer.
cori
A: 

we fetch the record correctly but

the record is in one line . how can we see it in different line ? can we use br i think it is not suit pls help its urgent

thanks in advance