PHP Connect MySQL


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. // 1. Create a database connection
  3. $connection = mysql_connect("localhost","root","");
  4. if (!$connection){
  5. die("Database connection failed: " . mysql_error());
  6. }
  7.  
  8. // 2. Select a database to use
  9. $db_select = mysql_select_db("widget_corp", $connection);
  10. if (!$db_select){
  11. die("Database selection failed: " . mysql_error());
  12. }
  13.  
  14. ?>
  15. <html>
  16. <head>
  17. <title>PHP with MySQL</title>
  18. </head>
  19. <body>
  20.  
  21. <?php
  22. // 3. Perform database query
  23. $result = mysql_query("SELECT * FROM subjects", $connection);
  24. if (!$result){
  25. die("Database query failed: " . mysql_error());
  26. }
  27.  
  28. // 4. Use returned data
  29. while ($row = mysql_fetch_array($result)){
  30. echo $row["menu_name"]." ".$row["position"]."<br />";
  31. }
  32.  
  33. ?>
  34.  
  35. </body>
  36. </html>
  37. <?php
  38. // 5. Close connection
  39. mysql_close($connection);
  40. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.