Advertisement
Guest User

Untitled

a guest
Aug 31st, 2012
50
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // This could be supplied by a user, for example
  3. $firstname = 'fred';
  4. $lastname = 'fox';
  5.  
  6. // Formulate Query
  7. // This is the best way to perform an SQL query
  8. // For more examples, see mysql_real_escape_string()
  9. $query = sprintf("SELECT firstname, lastname, address, age FROM friends
  10. WHERE firstname='%s' AND lastname='%s'",
  11. mysql_real_escape_string($firstname),
  12. mysql_real_escape_string($lastname));
  13.  
  14. // Perform Query
  15. $result = mysql_query($query);
  16.  
  17. // Check result
  18. // This shows the actual query sent to MySQL, and the error. Useful for debugging.
  19. if (!$result) {
  20. $message = 'Invalid query: ' . mysql_error() . "\n";
  21. $message .= 'Whole query: ' . $query;
  22. die($message);
  23. }
  24.  
  25. // Use result
  26. // Attempting to print $result won't allow access to information in the resource
  27. // One of the mysql result functions must be used
  28. // See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
  29. while ($row = mysql_fetch_assoc($result)) {
  30. echo $row['firstname'];
  31. echo $row['lastname'];
  32. echo $row['address'];
  33. echo $row['age'];
  34. }
  35.  
  36. // Free the resources associated with the result set
  37. // This is done automatically at the end of the script
  38. mysql_free_result($result);
  39. ?>
Advertisement
RAW Paste Data Copied
Advertisement