|
| |
|
|
craigj1303
Posts: 46 Joined: 5/16/2007 Status: offline
|
Problem with mysql_fetch_assoc() - 9/22/2009 9:06:33
Hi All I am following a PHP online tutorial and I am on a section that deals with reading from a MYSQL Database. I have loaded the below script which returns the following error: quote:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\read_database.php on line 12 Here is the code: $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);
} Can anyone advise what the problem could be? Thanks Craig
|
|
|
|
craigj1303
Posts: 46 Joined: 5/16/2007 Status: offline
|
RE: Problem with mysql_fetch_assoc() - 9/22/2009 18:02:19
Hi Tailslide I am running PHP 5.2.6. I am sorry but I am totally new to PHP and adding that code you suggested before my while loop just gave me parse error. Not being able to read from the database I moved on to the next section of the tutorial which involves adding records to the database. I run the below script and got no errors at all: <?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 = "INSERT INTO tb_address_book (First_Name, Surname, Address) VALUES ('bill', 'gates', 'Microsoft')";
$result = mysql_query($SQL);
mysql_close($db_handle);
print "Records added to the database";
}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}
?> So the problem must be something to do with the below line, does it not? quote:
$SQL = "SELECT * FROM tb_address_book";
|
|
|
|
craigj1303
Posts: 46 Joined: 5/16/2007 Status: offline
|
RE: Problem with mysql_fetch_assoc() - 9/24/2009 13:55:06
For those interested I sorted this problem. After hours and hours of head scratching I discovered that when I was setting up the database table in phpmyadmin I called it "tbl_address_book" (as instructed in the tutorial. If you look at the php script (also provided by the tutorial) it refers to the databast table as "tb_address_book"! So all the while it was a simple typo in the code provided! I wish php error messages were more comprehensive. I am a beginner but from what i've seen so far some of the error messages that come back scarecely point you in the direction of the error clearly. It would be nice if the error message said something like "the database table tb_address_book does not exist" for example.
|
|
|
|
rtwhite
Posts: 4 Joined: 9/25/2009 Status: offline
|
RE: Problem with mysql_fetch_assoc() - 9/25/2009 8:52:23
I appologize for getting into your thread. I am new to the forum and can't figure out how to create a new thread with a question. Can you give me some direction please? Thank you, Robert
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
|
|
|