What am I doing wrong?

Reply

Join Date: Jul 2007
Posts: 85
Reputation: Reliable is an unknown quantity at this point 
Solved Threads: 2
Reliable Reliable is offline Offline
Junior Poster in Training

What am I doing wrong?

 
0
  #1
Sep 9th, 2009
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:

  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.
Last edited by Ezzaral; Sep 9th, 2009 at 4:59 pm. Reason: Changed code tags for clarity.
Attached Images
File Type: bmp database.bmp (443.0 KB, 6 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 986
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 128
ardav's Avatar
ardav ardav is offline Offline
Posting Shark

Re: What am I doing wrong?

 
-1
  #2
Sep 9th, 2009
What's the problem? Any error messages?
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count - unless you're a serial downvoter.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 85
Reputation: Reliable is an unknown quantity at this point 
Solved Threads: 2
Reliable Reliable is offline Offline
Junior Poster in Training

Re: What am I doing wrong?

 
0
  #3
Sep 9th, 2009
Originally Posted by ardav View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1,335
Reputation: almostbob has a spectacular aura about almostbob has a spectacular aura about 
Solved Threads: 163
almostbob's Avatar
almostbob almostbob is online now Online
Nearly a Posting Virtuoso

Re: What am I doing wrong?

 
0
  #4
Sep 9th, 2009
  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
  1. $result = mysql_query("SELECT * FROM subjects" );
Failure is not an option It's included free
If at first you dont succeed, join the club
Of course its always in the last place you look, you dont keep looking after you find it

Please mark solved problems, solved
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC