topical media & game development

talk show tell print

professional-php-08-DB-connect.php / php



  <?php
  
  require_once('DB.php'); 
  dsn = 'pgsql://postgres@localhost/mydb';
  conn = DB::connect(dsn);
  
  if(DB::isError(conn)) {
    //you would probably want to do something a bit more graceful here
    print("Unable to connect to the database using the DSN dsn");
    die(conn->getMessage());
  }
  
  //get all results as an associative array
  conn->setFetchMode(DB_FETCHMODE_ASSOC);  
  
  sql = "SELECT * FROM mytable";
  data =& conn->getAll(sql); //returns all rows. Only use with small recordsets
  
  // Always check that data is not an error
  if (DB::isError(data)) {
      print("Error trying to run the query sql");
      die (data->getMessage());
  }
  
  var_dump(data);
  
  conn->disconnect(); //close the connection
  ?>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.