Exemplo n.º 1
0
 /**
  * Executes the query that was previously passed to the constructor.
  *
  * @param mixed  $arg      Query arguments to escape and insert at ? placeholders in $query
  * @param mixed  ...       Additional arguments
  **/
 public function execute()
 {
     // We can't very well perform a query wtihout an active connection.
     if ($this->dbh === false) {
         $this->db->error('Lost connection to database.');
         return;
     }
     // Finish any previous statements
     $this->finish();
     // Replace in the arguments
     $this->last_query = $this->replaceholders(Database::smart_args(func_get_args()));
     if (!$this->last_query) {
         return;
     }
     // Wrap the actual query execution in a bit of benchmarking.
     $before = microtime(true);
     $this->sh = mysql_query($this->last_query, $this->dbh);
     $after = microtime(true);
     $this->db->mysql_time += $after - $before;
     // Non-select statements return a boolean, which means we call affected_rows.
     if (is_bool($this->sh)) {
         $this->insert_id = mysql_insert_id($this->dbh);
         $this->affected_rows = mysql_affected_rows($this->dbh);
     } else {
         $this->num_rows = mysql_num_rows($this->sh);
     }
     // Can we pull down warnings from the server?  Because the mysql interface
     // doesn't give us the warning count, we have to check ourselves.
     $this->warnings = array();
     if ($this->db->enable_warning_logging) {
         if ($sh = mysql_query($this->dbh, 'SHOW WARNINGS')) {
             while ($row = mysql_fetch_row($sh)) {
                 $this->warnings[] = array('#' => $row[1], 'MSG' => $row[2]);
             }
             mysql_free_result($sh);
             // Push it upstream.
             $GLOBALS['_DEBUG']['Database Warnings'][] = array('Query' => $this->last_query, 'Warnings' => $this->warnings);
         }
     }
     // No matter what, a false statement handle means something horrid happened.
     if ($this->sh === false) {
         $this->db->error('SQL Error:');
         return false;
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * This takes the place of a database constructor.  It should be called directly
  * without an object as:
  *
  *      Database::connect(....)
  *
  * This assumes that you are either using a class autoloader (php5) or have
  * already require_once'd the appropriate database engine file
  * (eg. Database_mysql.php).
  *
  * @param string $db_name   Name of the database we're connecting to
  * @param string $login     Login name to use when connecting
  * @param string $password  Password to use when connecting
  * @param string $server    Database server to connect to           (default: localhost)
  * @param string $port      Port or socket address to connect to
  * @param string $engine    Database engine to use                  (default: mysql_detect)
  * @param array  $options   Hash of var=>value pairs of server options for
  *                          engines that support them
  *
  * @return object           Database subclass based on requested $engine
  **/
 static function &connect($db_name, $login, $password, $server = 'localhost', $port = NULL, $engine = 'mysql_detect', $options = array())
 {
     // For consistency, engine names are all lower case.
     $engine = strtolower($engine);
     // There are two versions of the mysql driver in php.  We have special
     // consideration here for people who want auto-detection.
     if ($engine == 'mysql_detect') {
         $dbh = new Database_mysql($db_name, $login, $password, $server, $port);
         // MySQL gets some extra smarts to try to use mysqli if it's available
         if ($dbh && function_exists('mysqli_connect')) {
             $version = preg_replace('/^(\\d+\\.\\d).*$/', '$1', $dbh->server_info());
             if ($version >= 4.1) {
                 $dbh->close();
                 $dbh = new Database_mysqlicompat($db_name, $login, $password, $server, $port);
             }
         }
     } else {
         $class = "Database_{$engine}";
         $dbh = new $class($db_name, $login, $password, $server, $port, $options);
     }
     // Set database connection to utf8
     $dbh->query('SET NAMES utf8;');
     // Make sure UNIX_TIMESTAMP AND FROM_UNIXTIME do the right things
     $dbh->query('SET time_zone="+0:00";');
     // Return
     return $dbh;
 }