Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 11th, 2012  |  syntax: None  |  size: 1.06 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. $conn = mysql_connect("localhost", "mysql_user", "mysql_password");
  4.  
  5. if (!$conn) {
  6.     echo "Unable to connect to DB: " . mysql_error();
  7.     exit;
  8. }
  9.  
  10. if (!mysql_select_db("mydbname")) {
  11.     echo "Unable to select mydbname: " . mysql_error();
  12.     exit;
  13. }
  14.  
  15. $sql = "SELECT id as userid, fullname, userstatus
  16.         FROM   sometable
  17.         WHERE  userstatus = 1";
  18.  
  19. $result = mysql_query($sql);
  20.  
  21. if (!$result) {
  22.     echo "Could not successfully run query ($sql) from DB: " . mysql_error();
  23.     exit;
  24. }
  25.  
  26. if (mysql_num_rows($result) == 0) {
  27.     echo "No rows found, nothing to print so am exiting";
  28.     exit;
  29. }
  30.  
  31. // While a row of data exists, put that row in $row as an associative array
  32. // Note: If you're expecting just one row, no need to use a loop
  33. // Note: If you put extract($row); inside the following loop, you'll
  34. //       then create $userid, $fullname, and $userstatus
  35. while ($row = mysql_fetch_assoc($result)) {
  36.     echo $row["userid"];
  37.     echo $row["fullname"];
  38.     echo $row["userstatus"];
  39. }
  40.  
  41. mysql_free_result($result);
  42.  
  43. ?>