Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include_once('better_mysqli.php'); // can be obtained from: http://pastebin.com/ATyzLUfK
- // == Instantiate the mysqli database object (aka open the database) ==
- $mysqli = new better_mysqli('your_server', 'your_user', 'your_pass', 'your_db_name');
- if (mysqli_connect_errno()) {
- error_log(sprintf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error()));
- exit;
- }
- // Uses the same constructor as mysqli so $mysqli can be used in all the normal ways that your used to
- // However, you can now do these cool things:
- // == basic select example ==
- $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
- while($sth->fetch()){
- // iterates once for each record found
- echo $row['col1'] .' and '. $row['col2'] .' are accessed like that.<br>';
- }
- // == basic looking but advanced behind the scenes select example ==
- // -- notice that we are using the * wildcard to select the columns
- // -- notice that the where clause parameter values are using the ? placeholder instead of an actual value
- // -- notice that the array passed in for the 3rd parameter contains, in order, the values to use for the ? placeholders
- // -- 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!
- $sth = $mysqli->select('select * from table1 where col1=? and col2=?', $row, array('col1_placeholder_value', 'col2_placeholder_value'));
- while($sth->fetch()){
- // iterates once for each record found
- // -- Notice that even though we specified the * wildcard column selector, we still access the column values by the column names
- // The better_mysqli class figured out the names for us which means we can add columns at a later date to the table without
- // worry that we would break any code that was dependent on the original column order. (aka: no dependency on column order)
- echo $row['col1'] .', '. $row['col2'] .', and '. $row['col_etc'] .' are accessed like that.<br>';
- }
- // The class prepares every unique statement just once and caches that statement handle for later use if needed.
- // So if we take the same example just used and put it inside a loop that changes the placeholder values each time
- // The statement handle is only ever prepared just once and re-used all other times
- // == select example preparing statement just once ==
- $example_values = array(
- 'foo' => 'bar',
- 'this' => 'that',
- 'one' => 'two'
- );
- foreach ($example_values as $col1_value => $col2_value){
- // Iterates 3 times:
- echo 'All records found using placeholder values ' .$col1_value. ' and ' .$col2_value. '<br>';
- $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
- while($sth->fetch()){
- // iterates once for each record found using the given placeholders values
- echo $row['col1'] .', '. $row['col2'] .', and '. $row['col_etc'] .' are accessed like that <br>';
- }
- }
- // You can get verbose debug info from the class too if you need
- $debug_level=10;
- unless( $sth = $mysqli->select('select col1, col2 from table1 where col1="example"', $row, array(), $debug_level, $string_to_store_verbose_debug_info) ){
- // something went wrong
- if($debug_level>0){
- echo $string_to_store_verbose_debug_info;
- }
- // .. do whatever error handling here
- }
- // insert, update, and delete statements are just as easy with this class ..
- // == insert example ==
- $statement = "insert into table1 (col1, col2, date_col, col_etc) values (?, ?, NOW(), ?)";
- $mysqli->insert($statement, array('col1_insert_value', 'col2_insert_value', 'col_etc_value'));
- // -- remember statements are prepared and cached so this costs less..
- $mysqli->insert($statement, array('another_col1_insert_value', 'another_col2_insert_value', 'another_col_etc_value'));
- // -- if you want the unique id of the record you are inserting do this
- $mysqli->insert($statement, array('col1_insert_value', 'col2_insert_value', 'col_etc_value'), $debug_level=0, $verbose_output, $id_of_new_record);
- echo 'The unique id of the recrod just inserted was: ' .$id_of_new_record. '<br>';
- // == update example ==
- $mysqli->update("update table1 set col1=? where col2=?", array('col1_value', 'col2_value'));
- // -- and of course you can get debug info
- $mysqli->update("update table1 set col1=? where col2=?", array('col1_value', 'col2_value'), $debug_level=10, $verbose_output);
- echo $verbose_output; // a detailed output of exatcly what is going on under the hood
- // == delete example ==
- $mysqli->delete("delete from table1 where col1=? where col2=?", array('col1_value', 'col2_value'));
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement