943,715 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 342
  • PHP RSS
Sep 9th, 2009
0

What am I doing wrong?

Expand Post »
I'm learning PHP & MySQL. While trying to folllow a tutorial I am not getting the erxpected result which is simply to retrieve a little data out of a table with a query.
Here is my PHP code:

php Syntax (Toggle Plain Text)
  1. <?php
  2. //1. Create a database connection
  3. $connection = mysql_connect("localhost", "kingdomc_mtech", "busine$$");
  4. if (!$connection)
  5. {
  6. die("Database connection failed: " . mysql_error());
  7. }
  8.  
  9. //2. Select a database to use
  10. $db_select = mysql_select_db("kingdomc_learning", $connection);
  11. if (!$db_select)
  12. {
  13. die ("Database selection failed: " . mysql_error());
  14. }
  15. ?>
  16.  
  17. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  18. <html xmlns="http://www.w3.org/1999/xhtml">
  19. <head>
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  21. <title>SQL lesson1</title>
  22. </head>
  23.  
  24. <body>
  25. <?php
  26. //3. Perform database query
  27. $result = mysql_query("SELECT * FROM subjects", $connection);
  28. if(!$result)
  29. {
  30. die ("Database query failed: " . mysql_error());
  31. }
  32. //4. Use returned data
  33. while ($row = mysql_fetch_array($result));
  34. {
  35. echo $row[0]." ".$row[2]."<br/>";
  36. }
  37. ?>
  38. </body>
  39. </html>
  40. <?php
  41. //5. Close connection
  42. mysql_close($connection);
  43. ?>

and i have attached a screenshot of my very simple table. Thanks for the time and help.
Attached Images
File Type: bmp database.bmp (443.0 KB, 11 views)
Last edited by Ezzaral; Sep 9th, 2009 at 4:59 pm. Reason: Changed code tags for clarity.
Similar Threads
Reputation Points: 11
Solved Threads: 3
Junior Poster
Reliable is offline Offline
152 posts
since Jul 2007
Sep 9th, 2009
-1

Re: What am I doing wrong?

What's the problem? Any error messages?
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 946
Sarcastic Poster
ardav is offline Offline
6,678 posts
since Oct 2006
Sep 9th, 2009
0

Re: What am I doing wrong?

Click to Expand / Collapse  Quote originally posted by ardav ...
What's the problem? Any error messages?
No error message. I'm not getting any data returned. When I load and even refresh my page its just blank.
Reputation Points: 11
Solved Threads: 3
Junior Poster
Reliable is offline Offline
152 posts
since Jul 2007
Sep 9th, 2009
0

Re: What am I doing wrong?

php Syntax (Toggle Plain Text)
  1. <?php
  2. //1. Create a database connection
  3. $connection = mysql_connect("localhost", "kingdomc_mtech", "busine$$");
  4. if (!$connection)
  5. {
  6. die("Database connection failed: " . mysql_error());
  7. }
  8.  
  9. //2. Select a database to use
  10. $db_select = mysql_select_db("kingdomc_learning", $connection);
  11. if (!$db_select)
  12. {
  13. die ("Database selection failed: " . mysql_error());
  14. }
  15. ?>
  16.  
  17. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  18. <html xmlns="http://www.w3.org/1999/xhtml">
  19. <head>
  20. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  21. <title>SQL lesson1</title>
  22. </head>
  23.  
  24. <body>
  25. <?php
  26. //3. Perform database query
  27. $result = mysql_query("SELECT * FROM subjects", $connection);
  28. if(!$result)
  29. {
  30. die ("Database query failed: " . mysql_error());
  31. }
  32. //4. Use returned data
  33. while ($row = mysql_fetch_array($result));
  34. {
  35. echo $row[0]." ".$row[2]."<br/>";
  36. }
  37. ?>
  38. </body>
  39. </html>
  40. <?php
  41. //5. Close connection
  42. mysql_close($connection);
  43. ?>
line 27, try
sql Syntax (Toggle Plain Text)
  1. $result = mysql_query("SELECT * FROM subjects" );
Reputation Points: 562
Solved Threads: 368
Posting Maven
almostbob is offline Offline
2,970 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: mobile redirect
Next Thread in PHP Forum Timeline: Simple text Verification





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC