PEAR::DB MySQL Functions


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



Copy this code and paste it in your HTML
  1. <?php
  2. // import PEAR DB libraries.
  3. require_once("DB.php");
  4.  
  5. // Connects to database, returns PEAR DB Object.
  6.  
  7. function connect() {
  8. $db =& DB::connect("mysql://user:password@host/database");
  9. if(DB::isError($db)) {
  10. die($db->getMessage());
  11. }
  12. return $db;
  13. }
  14.  
  15. // Returns a list of Arrays containing the results
  16. function execute_query($sql) {
  17. $conn = connect();
  18. $resultset = $conn->query($sql);
  19. if(PEAR::isError($resultset)) {
  20. die("Error: " . $resultset->getError());
  21. }
  22. $results = Array();
  23.  
  24. while($row =& $resultset->fetchRow(DB_FETCHMODE_ASSOC)) {
  25. $results[] = $row;
  26. }
  27. $conn->disconnect();
  28. return $results;
  29. }
  30. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.