Source for file DB_MySQLi.class.php

Documentation is available at DB_MySQLi.class.php

  1. <?php
  2.  
  3. // MinimalWeblog 1.0.2
  4. // Copyright 2006 Jan Pieter Kunst <jpkunst at xs4all dot nl>
  5. // 
  6. // Based on Personal Weblog 1.0.0
  7. // http://www.kyne.com.au/~mark/software/weblog.php
  8. // Copyright 2001,2002  Mark Pulford <mark@kyne.com.au>
  9. //
  10. // This program is free software; you can redistribute it and/or modify 
  11. // it under the terms of the GNU General Public License as published by 
  12. // the Free Software Foundation; either version 2 of the License, or 
  13. // (at your option) any later version. 
  14. //  
  15. // This program is distributed in the hope that it will be useful, 
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of 
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  18. // GNU General Public License for more details. 
  19. //  
  20. // You should have received a copy of the GNU General Public License 
  21. // along with this program; if not, write to the Free Software 
  22. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
  23.  
  24. /**
  25.  * @package MinimalWeblog
  26.  * @version 1.0.2
  27.  */
  28.  
  29. /**
  30.  * Class to make a connection to a MySQL database with the mysqli extension
  31.  *
  32.  * @package MinimalWeblog
  33.  */
  34. class DB_MySQLi {
  35.  
  36.     /**
  37.      * @var obj database connection object
  38.      */
  39.     private $_db_connection = FALSE;
  40.     /**
  41.      * @var string name of the weblog database
  42.      */
  43.     private $_database;
  44.  
  45.     /**
  46.      * Constructor
  47.      *
  48.      * @param string name of weblog database
  49.      * @param string host of weblog database
  50.      * @param string username for weblog database
  51.      * @param string password for weblog database
  52.      */
  53.     public function __construct($db$host$user$pass)    {
  54.         
  55.         if (extension_loaded('mysqli')) {
  56.             trigger_error('You need the "mysqli" extension to run this program'E_USER_ERROR);
  57.         }
  58.         
  59.         $this->_database = $db;
  60.         $this->_db_connection = mysqli_connect($host$user$pass);
  61.         
  62.         // Ensure the database exists
  63.         if (!mysqli_select_db($this->_db_connection$this->_database)) {
  64.             $this->_db_connection = FALSE;
  65.         }
  66.     }
  67.  
  68.     /**
  69.      * Method to perform queries to SELECT from the database
  70.      *
  71.      * @param string SQL query with placeholders if needed
  72.      * @param array array with values for placeholders
  73.      * @return array numeric array of found associative rows from the database
  74.      */
  75.     public function fetch($sql$params array())    {
  76.         // Must ensure correct DB is selected. If some other PHP
  77.         // code has run it might have changed the db in between
  78.         // Weblog() and insert().        
  79.         if (!mysqli_select_db($this->_db_connection$this->_database))
  80.             return;
  81.  
  82.         $array array();
  83.         
  84.         if (empty($params)) {
  85.             
  86.             $result mysqli_query($this->_db_connection$sql);
  87.             while ($row mysqli_fetch_assoc($result)) {
  88.                $array[$row;
  89.             }
  90.             mysqli_free_result($result);
  91.  
  92.         else {
  93.         
  94.             $stmt $this->_mysqli_stmt_prepare_execute($sql$params);
  95.             $result mysqli_stmt_result_metadata($stmt);
  96.             $fieldinfo mysqli_fetch_fields($result);
  97.                 
  98.             $vars array();
  99.             $row array();
  100.             foreach($fieldinfo as $val{
  101.                 // See http://www.php.net/manual/en/function.mysqli-stmt-bind-result.php
  102.                 // generate variables for storing result
  103.                 $vars[=$row[$val->name]
  104.             }
  105.  
  106.             // reference to statement must be first parameter of call to mysqli_stmt_bind_result
  107.             array_unshift($vars$stmt);
  108.             call_user_func_array('mysqli_stmt_bind_result'$vars);
  109.             $i 0;
  110.             while(mysqli_stmt_fetch($stmt)) {
  111.                 // $array[] = $row seems to assign a reference instead of a copy
  112.                 foreach($row as $k => $v{
  113.                     $array[$i][$k$v;
  114.                 }
  115.                 $i++;
  116.             }
  117.             
  118.             mysqli_stmt_close($stmt);
  119.         }
  120.  
  121.         return $array;
  122.     }
  123.  
  124.     /**
  125.      * Method to perform queries that change the database (INSERT, UPDATE, DELETE)
  126.      *
  127.      * @param string SQL query with placeholders
  128.      * @param array array with values for placeholders
  129.      * @return int number of affected rows
  130.      */
  131.     public function change($sql$params{
  132.  
  133.         if (!mysqli_select_db($this->_db_connection$this->_database))
  134.             return;
  135.         
  136.         $stmt $this->_mysqli_stmt_prepare_execute($sql$params);
  137.         $numrows mysqli_stmt_affected_rows($stmt);
  138.         mysqli_stmt_close($stmt);
  139.         
  140.         return $numrows;
  141.     }
  142.  
  143.     /**
  144.      * Private method to prepare and execute a mysqli prepared statement
  145.      *
  146.      * @param string SQL query with placeholders
  147.      * @param array array with values for placeholders
  148.      * @return obj mysqli_stmt object
  149.      */
  150.     private function _mysqli_stmt_prepare_execute($sql$params{
  151.     
  152.         $stmt mysqli_prepare($this->_db_connection$sql);
  153.         $types '';
  154.         $vars array();
  155.         foreach($params as $key => $param{
  156.             $vars[$params[$key];
  157.             if (is_integer($param)) {
  158.                 $types .= 'i';
  159.             elseif (is_string($param)) {
  160.                 $types .= 's';
  161.             }
  162.         }
  163.         
  164.         // string of types must be second parameter of call to mysqli_stmt_bind_param
  165.         array_unshift($vars$types);
  166.         // reference to statement must be first parameter of call to mysqli_stmt_bind_param
  167.         array_unshift($vars$stmt);
  168.         call_user_func_array('mysqli_stmt_bind_param'$vars);
  169.         mysqli_stmt_execute($stmt);
  170.  
  171.         return $stmt;
  172.     }
  173.  
  174.     /**
  175.      * Method to test if connection to database is established
  176.      *
  177.      * @return bool FALSE if not connected, TRUE otherwise
  178.      */
  179.     public function connected({
  180.         
  181.         if ($this->_db_connection === FALSE{
  182.             return FALSE;
  183.         else {
  184.             return TRUE;
  185.         }    
  186.     }
  187. }
  188.  
  189. ?>

Documentation generated on Sun, 17 Jun 2007 19:20:35 +0200 by phpDocumentor 1.3.2