Posted By

mikegreen on 05/22/08


Tagged

mysql database pear


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

matthall


PEAR::DB MySQL Functions


 / Published in: PHP
 

  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  

You need to login to post a comment.