info343/lectures/ajax-with-xml/files/db.php

<?php
/*   Derived from the WordPress adaptation of the EzSQL
 *   PHP class by Justin Vincent <justin@visunet.ie>
 *   http://php.justinvincent.com/
 * http://wordpress.org/
 */

define('DB_USER', 'animalgame', true);
define('DB_PASS', 'isyourobject??', true);
define('DB_NAME', 'animalgame', true);
define('DB_HOST', '127.0.0.1', true);

define('OBJECT', 'OBJECT', true);
define('ARRAY_A', 'ARRAY_A', false);
define('ARRAY_N', 'ARRAY_N', false);

class DB {
   var $show_errors = true;
   var $last_query;
   var $col_info;
   var $num_queries = 0;
   var $insert_id;
   
   function DB($user, $pass, $name, $host) {
      $this->dbh = @mysql_connect($host, $user, $pass);
      if (!$this->dbh)
         $this->bail("Error establishing database connection.");
      else
         $this->select($name);
   }
   
   function select($db) {
      if (!@mysql_select_db($db, $this->dbh)) {
         $this->bail("Can't select database.");
      }
   }
   
   function print_error($msg = '') {
      if (!$msg) $msg = mysql_error();
      if ($this->show_errors) {
         print '<p class="error"><strong>A database error occurred.</strong><br/>Query:<br/><code>' . $this->last_query . '</code><br/>Error:<br/><code>' . $msg . '</code></p>';
      }
   }
   
   function show_errors() {
      $this->show_errors = true;
   }
   
   function hide_errors() {
      $this->show_errors = false;
   }
   
   function flush() {
      $this->last_result = null;
      $this->col_info = null;
      $this->last_query = null;
   }
   
   function query($query) {
      $return_val = false;
      $this->flush();
      
      $this->last_query = $query;
      $this->result = @mysql_query($query, $this->dbh);
      $this->num_queries++;
      if (mysql_error()) {
         $this->print_error();
         return false;
      }
      if (preg_match("/^\\s*(insert|delete|update|replace) /i", $query)) {
         $this->affected_rows = mysql_affected_rows();
         if (preg_match("/^\\s*(insert|replace) /i", $query)) {
            $this->insert_id = mysql_insert_id($this->dbh);
         }
         $return_val = $this->affected_rows;
      }
      else {
         $i = 0;
         while ($i < @mysql_num_fields($this->result)) {
            $this->col_info[$i] = @mysql_fetch_field($this->result, $i);
            $this->col_info[$i]->length = $this->col_info[$i]->max_length;
            $this->col_info[$i]->max_length = @mysql_field_len($this->result, $i);
            $i++;
         }
         $num_rows = 0;
         while ($row = @mysql_fetch_object($this->result)) {
            $this->last_result[$num_rows] = $row;
            $num_rows++;
         }
         
         @mysql_free_result($this->result);
         $this->num_rows = $num_rows;
         $return_val = $this->num_rows;
      }
      
      return $return_val;
   }
   
   function get_var($query = null, $x = 0, $y = 0) {
      if ($query)
         $this->query($query);
      
      if ($this->last_result[$y])
         $values = array_values(get_object_vars($this->last_result[$y]));
      
      return (isset($values[$x]) && $values[$x] !== '') ? $values[$x] : null;
   }
   
   function get_row($query = null, $output = OBJECT, $y = 0) {
      if ($query)
         $this->query($query);
      
      if ($output == OBJECT)
         return $this->last_result[$y] ? $this->last_result[$y] : null;
      else if ($output == ARRAY_A)
         return $this->last_result[$y] ? get_object_vars($this->last_result[$y]) : null;
      else if ($output == ARRAY_N)
         return $this->last_result[$y] ? array_values(get_object_vars($this->last_result[$y])) : null;
      else
         $this->print_error("Unknown output type: '$output'. Must be one of: OBJECT, ARRAY_A, ARRAY_N");
   }
   
   function get_col($query = null, $x = 0) {
      if ($query)
         $this->query($query);
      
      for ($i = 0; $i < count($this->last_result); $i++) {
         $new_array[$i] = $this->get_var(null, $x, $i);
      }
      return $new_array;
   }
   
   function make_assoc_array($query = null, $x1 = 0, $x2 = 1) {
      if ($query)
         $this->query($query);
      
      if (count($this->last_result)) {
         for ($i = 0; $i < count($this->last_result); $i++) {
            $new_array[$this->get_var(null, $x1, $i)] = $this->get_var(null, $x2, $i);
         }
         return $new_array;
      }
      else {
         return null;
      }
   }
   
   function get_results($query = null, $output = OBJECT) {
      if ($query)
         $this->query($query);
      
      if ($output == OBJECT) {
         return $this->last_result;
      }
      else if ($output == ARRAY_A || $output == ARRAY_N) {
         if ($this->last_result) {
            $i = 0;
            foreach ($this->last_result as $row) {
               $new_array[$i] = (array) $row;
               if ($output == ARRAY_N) {
                  $new_array[$i] = array_values($new_array[$i]);
               }
               $i++;
            }
            return $new_array;
         }
         else {
            return null;
         }
      }
   }
   
   function explain() {
      if ($this->col_info) {
         foreach ($this->col_info as $col) {
            $new_array[$col->name] = $col;
         }
         return $new_array;
      }
   }
   
   function get_col_info($info_type = 'name', $col_offset = -1) {
      if ($this->col_info) {
         if ($col_offset == -1) {
            $i = 0;
            foreach ($this->col_info as $col) {
               $new_array[$i] = $col->{$info_type};
               $i++;
            }
            return $new_array;
         }
         else {
            return $this->col_info[$col_offset]->{$info_type};
         }
      }
   }
   
   function bail($msg) {
      if ($this->show_errors)
         $msg = "<h1>Database Error:</h1><p>$msg</p><p><code>" . mysql_error() . "</code></p>";
      else
         $msg = "";
      
      die($msg);
   }
}

$db = new DB(DB_USER, DB_PASS, DB_NAME, DB_HOST);

?>