Advertisement
KzDrew

better_mysqli_basic_usage.php

Jul 23rd, 2013
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 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.  
  14.   // == select example ==
  15.   unless( $sth = $mysqli->select('select * from table1 where col1=? and col2=?', $row, array('col1_placeholder_value', 'col2_placeholder_value'), $debug_level=0, $verbose_debug_output)){
  16.     if($debug_level>0){ echo $verbose_debug_output;}
  17.     // .. do your error handling here
  18.   }
  19.   while($sth->fetch()){
  20.       echo $row['col1'] .', '. $row['col2'] .', and '. $row['col_etc'] .' are accessed like that.<br>';
  21.   }
  22.  
  23.  
  24.   // == insert example ==
  25.   $statement = "insert into table1 (col1, col2, date_col, col_etc) values (?, ?, NOW(), ?)";
  26.   unless( $mysqli->insert($statement, array('col1_insert_value', 'col2_insert_value', 'col_etc_value'), $debug_level=0, $verbose_debug_output, $id_of_new_record) ){     
  27.       if($debug_level>0){ echo $verbose_debug_output;}
  28.       // .. do your error handling here
  29.   }
  30.  
  31.  
  32.   // == update example ==
  33.   unless($mysqli->update("update table1 set col1=? where col2=?", array('col1_value', 'col2_value'), $debug_level=0, $verbose_debug_output) ){
  34.       if($debug_level>0){ echo $verbose_debug_output;}
  35.       // .. do your error handling here  
  36.   }
  37.  
  38.  
  39.   // == delete example ==
  40.   unless( $mysqli->delete("delete from table1 where col1=? where col2=?", array('col1_value', 'col2_value'), $debug_level=0, $verbose_debug_output) ){
  41.       if($debug_level>0){ echo $verbose_debug_output;}
  42.       // .. do your error handling here  
  43.   }
  44.  
  45.  
  46.   // in all cases statements are prepared just once and cached so if you reuse any statement the already prepared statement handle is automatically used
  47.  
  48.  
  49. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement