Posted By

psicosis on 01/21/11


Tagged

mysql php


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

diskostu


MySQL DB connect


 / Published in: PHP
 

  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 database 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.  
  21. <?php
  22. $result = mysql_query("SELECT * FROM tablename", $connection);
  23.  
  24. while ($row = mysql_fetch_array($result))
  25. {
  26. echo $row["rownamehere"]." ".$row["rownamehere"]."<br/>";
  27. }
  28. ?>
  29.  
  30. </body>
  31.  
  32. <?php
  33. mysql_close($connection);
  34. ?>

Report this snippet  

You need to login to post a comment.