Get support from Xavier Media
It is currently Sun Dec 08, 2013 12:25 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Please WHO can help me?
PostPosted: Wed Sep 10, 2008 2:09 pm 
Offline
Rookie (I'm probably spamming)
Rookie (I'm probably spamming)

Joined: Tue Sep 09, 2008 11:02 am
Posts: 1
Points: 0
Please WHO can help me?
I have this code that i am learning that I got from a PHP tut.

When I write the code up to a certain point I get a favorable result. When I now add the code to get the data from my database i get an error message. Please tell me what should I be doing or not doing? Here is the code:-

PHP Code:
<?PHP

$user_name = "root";
$password = "";
//$database="checking";
$database = "address_book";
$server = "127.0.0.1";

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

$db_found = mysql_select_db($database, $db_handle);

print "Connection to the Server opened.";

if ($db_found) {

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

print "Database NOT Found";
}
?>

The code above gives me a favorable result. When I run the code with EASYPHP I get the following result
Connection to the Server opened.Database Found

*** This is where the problem now comes up. When I add this code i get an error message.

PHP Code:
[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>";
}

I get the following error message[Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Program Files\EasyPHP 2.0b1\www\docs\prac\webdesign\connect_test.php on line 25

Following is the full script and code that gives me the error message above



PHP Code:
<?PHP

$user_name = "root";
$password = "";
//$database="checking";
$database = "testdb";
$server = "127.0.0.1";

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

$db_found = mysql_select_db($database, $db_handle);

print "Connection to the Server opened.";

if ($db_found) {

print "Database 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";
}
?>

I now get the following message

[Connection to the Server opened.Database Found
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\Program Files\EasyPHP 2.0b1\www\docs\prac\webdesign\connect_test.php on line 25

Please who can help me? I am just learning PHP. Thanks.


Report this post
Top
 Profile Send private message E-mail  
Reply with quote  
 Post subject: Re: Please WHO can help me?
PostPosted: Wed Sep 10, 2008 5:53 pm 
Offline
XavierForum.com Admin
XavierForum.com Admin
Super VIP Member
Super VIP Member
User avatar

Joined: Tue Mar 15, 2005 4:37 pm
Posts: 1082
Location: Sweden
Points: 1500
There's something wrong with your SQL code. Make sure the table is actually called tb_address_book.

/Andreas

_________________
Xavier Media®
Join me at LinkedIn | Get your latest blog post in your signature you too


Report this post
Top
 Profile Send private message  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Registered users: No registered users


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  

Portal » Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
[
SEO MOD © 2007 StarTrekGuide ]