PHP connecting and query MySQL DB


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



Copy this code and paste it in your HTML
  1. <?php
  2. //create db connection
  3. $connection = mysql_connect("localhost", "user", "password");
  4. if(!$connection)
  5. {
  6. die("Database connection failed:" . mysql_error());
  7. }
  8.  
  9. // select a dtabase to use
  10. $db_select = mysql_select_db("database name", $connection);
  11. if(!$db_select)
  12. {
  13. die("Database selection failed:" . mysql_error());
  14. }
  15.  
  16.  
  17. ?>
  18.  
  19. <body>
  20. $result = mysql_query("SELECT * FROM tablename", $connection);
  21.  
  22. while ($row = mysql_fetch_array($result))
  23. {
  24. echo $row["rownamehere"]." ".$row["rownamehere"]."<br/>";
  25. }
  26. ?>
  27. </body>
  28.  
  29. <?php
  30. mysql_close($connection);
  31. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.