OutFront Forums
     Home    Register     Search      Help      Login    

Follow Us
On Facebook
On Twitter
RSS
Via Email

Recent Posts
Todays Posts
Most Active posts
Posts since last visit
My Recent Posts
Mark posts read

Sponsors
Shopping Cart Software
Ecommerce software integrated into Frontpage, Dreamweaver and Golive templates. No monthly fees and available in ASP and PHP versions.
Website Templates
We also have a wide selection of Dreamweaver, Expression Web and Frontpage templates as well as webmaster tools and CSS layouts.
Frontpage website templates
Creative Website Templates for FrontPage, Dreamweaver, Flash, SwishMax

 

Problem with mysql_fetch_assoc()

 
View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
Printable Version 

All Forums >> Web Development >> ASP, PHP, and Database >> Problem with mysql_fetch_assoc()
Page: [1]
 
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
Tailslide

 

Posts: 6692
Joined: 5/10/2005
From: Out here on the raggedy edge
Status: offline

 
RE: Problem with mysql_fetch_assoc() - 9/22/2009 9:37:36   
Hmmm... looks like it's looking for a true/false answer somewhere in there... something wrong with the SQL query bit.

You might want to check which version of PHP you're running too as that script says it's for older than PHP5.

If you get no joy have a look here: http://www.tizag.com/phpT/ Tizag's tutorials are great and usually easy to follow.

Ok so try adding:

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



Before your While loop. Might give some more info on the error. That is assuming that the table is actually there and that the password and the other details are correct.

< Message edited by Tailslide -- 9/22/2009 10:09:39 >


_____________________________

Little Blue Plane Web Design | Land Rover project

:)

(in reply to craigj1303)
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";

(in reply to Tailslide)
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.


(in reply to craigj1303)
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

(in reply to craigj1303)
Page:   [1]

All Forums >> Web Development >> ASP, PHP, and Database >> Problem with mysql_fetch_assoc()
Page: [1]
Jump to: 1





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