We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

mswallace on 03/18/08


Tagged

mysql database php query connection


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

Wiederkehr
bsisco
Nix


PHP connecting and query MySQL DB


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 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 

You need to login to post a comment.