Advertisement
KzDrew

better_mysqli_detailed_example.php

Jul 23rd, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.94 KB | None | 0 0
  1. <?php
  2.  
  3.   include_once('better_mysqli.php');           // can be obtained from: http://pastebin.com/ATyzLUfK
  4.  
  5.  
  6.   // == Instantiate the mysqli database object (aka open the database) ==
  7.   $mysqli = new better_mysqli('your_server', 'your_user', 'your_pass', 'your_db_name');
  8.   if (mysqli_connect_errno()) {
  9.      error_log(sprintf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error()));
  10.      exit;
  11.   }
  12.  
  13.   // Uses the same constructor as mysqli so $mysqli can be used in all the normal ways that your used to
  14.  
  15.   // However, you can now do these cool things:
  16.  
  17.   // == basic select example ==
  18.   $sth = $mysqli->select('select col1, col2 from table1 where col1="example"', $row, array());  // ignore the empty array passed in as the 3rd param for now, but it is required
  19.   while($sth->fetch()){
  20.       // iterates once for each record found
  21.       echo $row['col1'] .' and '. $row['col2'] .' are accessed like that.<br>';
  22.   }
  23.  
  24.  
  25.   // == basic looking but advanced behind the scenes select example ==
  26.   //    -- notice that we are using the * wildcard to select the columns
  27.   //    -- notice that the where clause parameter values are using the ? placeholder instead of an actual value
  28.   //    -- notice that the array passed in for the 3rd parameter contains, in order, the values to use for the ? placeholders
  29.   //    -- notice that there are no clunky bind_param calls with it's cryptic 'sssiiis' type string.  All that hassle was automatically handled by the class!
  30.   $sth = $mysqli->select('select * from table1 where col1=? and col2=?', $row, array('col1_placeholder_value', 'col2_placeholder_value'));
  31.   while($sth->fetch()){
  32.       // iterates once for each record found
  33.       // -- Notice that even though we specified the * wildcard column selector, we still access the column values by the column names
  34.       //    The better_mysqli class figured out the names for us which means we can add columns at a later date to the table without
  35.       //    worry that we would break any code that was dependent on the original column order. (aka: no dependency on column order)
  36.  
  37.       echo $row['col1'] .', '. $row['col2'] .', and '. $row['col_etc'] .' are accessed like that.<br>';
  38.   }
  39.  
  40.  
  41.   // The class prepares every unique statement just once and caches that statement handle for later use if needed.
  42.   // So if we take the same example just used and put it inside a loop that changes the placeholder values each time
  43.   // The statement handle is only ever prepared just once and re-used all other times
  44.  
  45.   // == select example preparing statement just once ==
  46.   $example_values = array(
  47.     'foo' => 'bar',
  48.     'this' => 'that',
  49.     'one' => 'two'
  50.   );
  51.   foreach ($example_values as $col1_value => $col2_value){
  52.     // Iterates 3 times:
  53.     echo 'All records found using placeholder values ' .$col1_value. ' and ' .$col2_value. '<br>';
  54.     $sth = $mysqli->select('select * from table1 where col1=? and col2=?', $row, array('col1_placeholder_value', 'col2_placeholder_value')); // statement is actually only ever prepared once
  55.     while($sth->fetch()){
  56.         // iterates once for each record found using the given placeholders values
  57.         echo $row['col1'] .', '. $row['col2'] .', and '. $row['col_etc'] .' are accessed like that <br>';
  58.     }
  59.   }
  60.  
  61.  
  62.   // You can get verbose debug info from the class too if you need
  63.   $debug_level=10;
  64.   unless( $sth = $mysqli->select('select col1, col2 from table1 where col1="example"', $row, array(), $debug_level, $string_to_store_verbose_debug_info) ){
  65.       // something went wrong
  66.       if($debug_level>0){
  67.         echo $string_to_store_verbose_debug_info;
  68.       }
  69.       // .. do whatever error handling here
  70.   }
  71.  
  72.  
  73.   // insert, update, and delete statements are just as easy with this class ..
  74.  
  75.  
  76.   // == insert example ==
  77.   $statement = "insert into table1 (col1, col2, date_col, col_etc) values (?, ?, NOW(), ?)";
  78.   $mysqli->insert($statement, array('col1_insert_value', 'col2_insert_value', 'col_etc_value'));
  79.  
  80.   // -- remember statements are prepared and cached so this costs less..
  81.   $mysqli->insert($statement, array('another_col1_insert_value', 'another_col2_insert_value', 'another_col_etc_value'));
  82.  
  83.   // -- if you want the unique id of the record you are inserting do this
  84.   $mysqli->insert($statement, array('col1_insert_value', 'col2_insert_value', 'col_etc_value'), $debug_level=0, $verbose_output, $id_of_new_record);
  85.   echo 'The unique id of the recrod just inserted was: ' .$id_of_new_record. '<br>';
  86.  
  87.  
  88.  
  89.   // == update example ==
  90.   $mysqli->update("update table1 set col1=? where col2=?", array('col1_value', 'col2_value'));
  91.   // -- and of course you can get debug info
  92.   $mysqli->update("update table1 set col1=? where col2=?", array('col1_value', 'col2_value'), $debug_level=10, $verbose_output);
  93.   echo $verbose_output; // a detailed output of exatcly what is going on under the hood
  94.  
  95.  
  96.  
  97.   // == delete example ==
  98.   $mysqli->delete("delete from table1 where col1=? where col2=?", array('col1_value', 'col2_value'));
  99.  
  100.  
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement