Connecting to a MySQL DB


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



Copy this code and paste it in your HTML
  1. // (1) Open the database connection
  2.  
  3. $connection = mysql_connect("localhost","fred","shhh");
  4.  
  5.  
  6.  
  7. // (2) Select the winestore database
  8.  
  9. mysql_select_db("winestore", $connection);
  10.  
  11.  
  12.  
  13. // (3) Run the query on the winestore through the connection
  14.  
  15. $result = mysql_query ("SELECT * FROM
  16.  
  17. wine", $connection);
  18.  
  19.  
  20.  
  21. // (4) While there are still rows in the result set, fetch the current
  22.  
  23. // row into the array $row
  24.  
  25. while ($row = mysql_fetch_array($result, MYSQL_NUM))
  26.  
  27. {
  28.  
  29. // (5) Print out each element in $row, that is, print the values of
  30.  
  31. // the attributes
  32.  
  33. foreach ($row as $attribute)
  34.  
  35. print "{$attribute} ";
  36.  
  37.  
  38.  
  39. // Print a carriage return to neaten the output
  40.  
  41. print "\n";
  42.  
  43. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.