If you're trying to get started for the first time, read the Quick Start Guide first!
1 2 3 4 5 6 | DB:: $user = 'my_database_user' ; DB:: $password = 'my_database_password' ; DB:: $dbName = 'my_database_name' ; DB:: $host = '123.111.10.23' ; //defaults to localhost if omitted DB:: $port = '12345' ; // defaults to 3306 if omitted DB:: $encoding = 'utf8' ; // defaults to latin1 if omitted |
1 2 | DB:: $ssl = array ( 'key' => '' , 'cert' => '' , 'ca_cert' => '' , 'ca_path' => '' , 'cipher' => '' ); DB:: $connect_options = array (MYSQLI_OPT_CONNECT_TIMEOUT => 10); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | DB:: $error_handler = 'my_error_handler' ; function my_error_handler( $params ) { echo "Error: " . $params [ 'error' ] . "<br>\n" ; echo "Query: " . $params [ 'query' ] . "<br>\n" ; die ; // don't want to keep going if a query broke } // this broken query will cause my_error_handler() to run DB::query( "SELCT * FROM accounts" ); // misspelled SELECT class Errors { public static function static_error_handler( $params ) { echo "Error: " . $params [ 'error' ] . "<br>\n" ; echo "Query: " . $params [ 'query' ] . "<br>\n" ; die ; // don't want to keep going if a query broke } public function error_handler( $params ) { echo "Error: " . $params [ 'error' ] . "<br>\n" ; echo "Query: " . $params [ 'query' ] . "<br>\n" ; die ; // don't want to keep going if a query broke } } // use a static class method as an error handler DB:: $error_handler = array ( 'Errors' , 'static_error_handler' ); // use an object method as an error handler $my_object = new Errors(); DB:: $error_handler = array ( $my_object , 'error_handler' ); //restore default error handler DB:: $error_handler = true; //ignore errors (BAD IDEA) DB:: $error_handler = false; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | DB:: $error_handler = false; // since we're catching errors, don't need error handler DB:: $throw_exception_on_error = true; try { // try to insert something with a primary key that already exists // will cause an exception to get thrown, and we'll catch it DB::insert( 'accounts' , array ( 'id' => 2, // duplicate primary key 'username' => 'Joe' , 'password' => 'asd254890s' )); } catch (MeekroDBException $e ) { echo "Error: " . $e ->getMessage() . "<br>\n" ; // something about duplicate keys echo "SQL Query: " . $e ->getQuery() . "<br>\n" ; // INSERT INTO accounts... } // restore default error handling behavior // don't throw any more exceptions, and die on errors DB:: $error_handler = 'meekrodb_error_handler' ; DB:: $throw_exception_on_error = false; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | DB:: $success_handler = true; // echo out each SQL command being run, and the runtime $results = DB::query( "SELECT * FROM accounts WHERE password=%s" , 'hello' ); // some command DB:: $success_handler = 'my_success_handler' ; // run this function after each successful command function my_success_handler( $params ) { echo "Command: " . $params [ 'query' ] . "<br>\n" ; echo "Time To Run It: " . $params [ 'runtime' ] . " (milliseconds)<br>\n" ; } $results = DB::query( "SELECT * FROM accounts" ); // some command class Success { public static function static_success_handler() { echo "Command: " . $params [ 'query' ] . "<br>\n" ; echo "Time To Run It: " . $params [ 'runtime' ] . " (milliseconds)<br>\n" ; } public function success_handler() { echo "Command: " . $params [ 'query' ] . "<br>\n" ; echo "Time To Run It: " . $params [ 'runtime' ] . " (milliseconds)<br>\n" ; } } // use a static class method as an success handler DB:: $success_handler = array ( 'Success' , 'static_success_handler' ); // use an object method as an success handler $my_object = new Success(); DB:: $success_handler = array ( $my_object , 'success_handler' ); DB:: $success_handler = false; // disable success handler |
1 2 3 4 5 6 7 8 | // don't want the DATE_FORMAT string to be evaluated by MeekroDB // pass it directly to MySQL as written instead, and use ##i to refer // to the MeekroDB 'integer' (which is normally %i) DB:: $param_char = '##' ; $row = DB::queryFirstRow( "SELECT DATE_FORMAT( c.sent, '%b %d %h:%i %p' ) FROM `call` c WHERE pk=##i", 4 ); DB:: $param_char = '%' ; // revert to normal behavior |
1 2 3 4 5 | DB:: $usenull = false; DB::insert( 'accounts' , array ( 'username' => 'Joe' , 'password' => null // will be set to empty string )); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | DB:: $nested_transactions = true; $depth = DB::startTransaction(); echo "We are now " . $depth . " transactions deep.\n" ; // 1 DB::query( "UPDATE accounts SET weight=%i WHERE username=%s" , 150, 'Joe' ); $depth = DB::startTransaction(); echo "We are now " . $depth . " transactions deep.\n" ; // 2 DB::query( "UPDATE accounts SET weight=%i WHERE username=%s" , 160, 'Joe' ); $depth = DB::rollback(); // rollback just the inner transaction echo "We are now " . $depth . " transactions deep.\n" ; // 1 $depth = DB::commit(); // commit the outer transaction echo "We are now " . $depth . " transactions deep.\n" ; // 0 $weight = DB::queryFirstField( "SELECT weight FROM accounts WHERE username=%s" , 'Joe' ); echo "Joe's weight is " . $weight . "\n" ; // 150 |
1 2 3 4 5 6 7 8 | DB:: $nested_transactions = true; DB::startTransaction(); $depth = DB::startTransaction(); // $depth is 2 $depth = DB::transactionDepth(); // $depth is still 2 |
1 2 3 4 5 6 7 8 | DB:: $nested_transactions = true; DB::startTransaction(); DB::startTransaction(); $depth = DB::transactionDepth(); // $depth is 2 DB::commit(true); $depth = DB::transactionDepth(); // $depth is 0 |
Placeholder Variables | |
---|---|
%s | string |
%i | integer |
%d | decimal/double |
%t | timestamp (can be instance of DateTime or string accepted by strtotime) |
%? | any data type (including arrays) -- will automatically do the right thing |
%ss | search string (string surrounded with % for use with LIKE) |
%b | backticks (can be dangerous with user-supplied data -- BE CAREFUL) |
%l | literal (no escaping or parsing of any kind -- BE CAREFUL) |
%ls | list of strings (array) |
%li | list of integers |
%ld | list of decimals/doubles |
%lt | list of timestamps |
%lb | list of backticks (can be dangerous with user-supplied data -- BE CAREFUL) |
%ll | list of literals (no escaping or parsing of any kind -- BE CAREFUL) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | // no placeholders DB::query( "SELECT * FROM tbl" ); // string, integer, and decimal placeholders DB::query( "SELECT * FROM tbl WHERE name=%s AND age > %i AND height <= %d" , $name , 15, 13.75); // use the parameter number to refer to parameters out of order DB::query( "SELECT * FROM tbl WHERE name=%s2 AND age > %i0 AND height <= %d1" , 15, 13.75, $name ); // use named parameters DB::query( "SELECT * FROM tbl WHERE name=%s_name AND age > %i_age AND height <= %d_height" , array ( 'name' => $name , 'age' => 15, 'height' => 13.75 ) ); // use %b and %? to construct an UPDATE // also use timestamp -- you can do this with the %t parameter, too DB::query( "UPDATE %b0 SET %?2 WHERE id=%i1" , 'tbl' , 5, array ( 'age' => 7, 'name' => $name , 'timestamp' => new DateTime( "now" ) ) ); // list of strings and list of integers placeholders $results = DB::query( "SELECT * FROM tbl WHERE name IN %ls AND age NOT IN %li" , array ( 'John' , 'Bob' ), array (12, 15)); // using the results from the last query // you get an array of associative arrays, so you can interate over the rows // with foreach foreach ( $results as $row ) { echo "Name: " . $row [ 'name' ] . "\n" ; echo "Age: " . $row [ 'age' ] . "\n" ; echo "Height: " . $row [ 'height' ] . "\n" ; echo "-------------\n" ; } |
1 2 3 4 | // get information on the account with the username Joe $account = DB::queryFirstRow( "SELECT * FROM accounts WHERE username=%s" , 'Joe' ); echo "Username: " . $account [ 'username' ] . "\n" ; // will be Joe, obviously echo "Password: " . $account [ 'password' ] . "\n" ; |
1 2 3 4 | // get information on the account with the username Joe list( $username , $password ) = DB::queryFirstList( "SELECT username, password FROM accounts WHERE username=%s" , 'Joe' ); echo "Username: " . $username . "\n" ; // will be Joe, obviously echo "Password: " . $password . "\n" ; |
1 2 3 4 5 | // get a list of DISTINCT usernames in the accounts table (skip duplicates, if any) $usernames = DB::queryFirstColumn( "SELECT DISTINCT username FROM accounts" ); foreach ( $usernames as $username ) { echo "Username: " . $username . "\n" ; } |
1 2 3 4 5 | // get a list of ALL usernames in the accounts table $usernames = DB::queryOneColumn( 'username' , "SELECT * FROM accounts" ); foreach ( $usernames as $username ) { echo "Username: " . $username . "\n" ; } |
1 2 3 | // get Joe's password and print it out $joePassword = DB::queryFirstField( "SELECT password FROM accounts WHERE username=%s" , 'Joe' ); echo "Joe's password is: " . $joePassword . "\n" ; |
1 2 3 | // get Joe's password and print it out $joePassword = DB::queryOneField( 'password' , "SELECT * FROM accounts WHERE username=%s" , 'Joe' ); echo "Joe's password is: " . $joePassword . "\n" ; |
1 2 3 4 5 6 7 8 9 10 11 12 | $joe = DB::queryFullColumns( "SELECT * FROM accounts WHERE username=%s" , 'Joe' ); print_r( $joe ); /* Returns something like: Array ( [accounts.id] => 3 [accounts.username] => Joe [accounts.password] => whatever ) */ |
1 2 3 | $mysqli_result = DB::queryRaw( "SELECT * FROM accounts WHERE username=%s" , 'Joe' ); $row = $mysqli_result ->fetch_assoc(); echo "Joe's password is: " . $row ['password'] . "\n" ; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | // insert a new account DB::insert( 'accounts' , array ( 'username' => 'Joe' , 'password' => 'hello' )); // change Joe's password (assuming username is a primary key) DB::replace( 'accounts' , array ( 'username' => 'Joe' , 'password' => 'asd254890s' )); // use DB::sqleval() to pass things directly to MySQL // sqleval() supports the same parameter structure as query() DB::insert( 'accounts' , array ( 'username' => 'Joe' , 'password' => 'hello' , 'data' => DB::sqleval( "REPEAT('blah', %i)" , 4), // REPEAT() is evaluated by MySQL 'time' => DB::sqleval( "NOW()" ) // NOW() is evaluated by MySQL )); // insert two rows at once $rows = array (); $rows [] = array ( 'username' => 'Frankie' , 'password' => 'abc' ); $rows [] = array ( 'username' => 'Bob' , 'password' => 'def' ); DB::insert( 'accounts' , $rows ); |
1 2 3 4 5 6 | // insert new account, don't throw an error if primary key id is already taken DB::insertIgnore( 'accounts' , array ( 'id' => 5, //primary key 'username' => 'Joe' , 'password' => 'hello' )); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // insert new account, if id 5 is already taken // then change Joe's password to goodbye instead DB::insertUpdate( 'accounts' , array ( 'id' => 5, //primary key 'username' => 'Joe' , 'password' => 'hello' ), 'password=%s' , 'goodbye' ); // same as above DB::insertUpdate( 'accounts' , array ( 'id' => 5, //primary key 'username' => 'Joe' , 'password' => 'hello' ), array ( 'password' => 'goodbye' )); // insert new account, if id 5 is taken then the username and password fields // will be set to 'Joe' and 'hello' respectively and all other fields ignored // this is a bit like REPLACE INTO, except we leave any other columns in the table // untouched DB::insertUpdate( 'accounts' , array ( 'id' => 5, //primary key 'username' => 'Joe' , 'password' => 'hello' )); |
1 2 3 4 5 6 7 8 9 10 | // change Joe's password DB::update( 'accounts' , array ( 'password' => 'sdfdd' ), "username=%s" , 'Joe' ); // set Joe's password to "joejoejoe" // WARNING: Passing user-submitted data to sqleval() will probably create a security flaw!! DB::update( 'accounts' , array ( 'password' => DB::sqleval( "REPEAT('joe', 3)" ) ), "username=%s" , 'Joe' ); |
1 2 | // delete Joe's account DB:: delete ( 'accounts' , "username=%s" , 'Joe' ); |
1 2 3 4 5 6 7 8 | // insert a new account DB::insert( 'accounts' , array ( 'id' => 0, // auto incrementing column 'username' => 'Joe' , 'password' => 'hello' )); $joe_id = DB::insertId(); // which id did it choose?!? tell me!! |
1 2 3 | DB::query( "SELECT * FROM accounts WHERE password=%s" , 'hello' ); $counter = DB:: count (); echo $counter . " people are using hello as their password!!\n" ; |
1 2 3 4 | // give a better password to everyone who is using hello as their password DB::query( "UPDATE accounts SET password=%s WHERE password=%s" , 'sdfwsert4rt' , 'hello' ); $counter = DB::affectedRows(); echo $counter . " people just got their password changed!!\n" ; |
1 2 3 4 5 6 7 8 9 10 11 | DB::debugMode(); // echo out each SQL command being run, and the runtime $results = DB::query( "SELECT * FROM accounts WHERE password=%s" , 'hello' ); // some command DB::debugMode( 'my_debugmode_handler' ); // run this function after each successful command function my_debugmode_handler( $params ) { echo "Command: " . $params [ 'query' ] . "<br>\n" ; echo "Time To Run It: " . $params [ 'runtime' ] . " (milliseconds)<br>\n" ; } $results = DB::query( "SELECT * FROM accounts" ); // some command DB::debugMode(false); // disable debug mode |
1 2 | DB::useDB( 'my_other_database' ); $result = DB::query( "SELECT * FROM my_table" ); |
1 2 3 4 5 6 7 8 9 10 11 12 | // give a better password to everyone who is using hello as their password // but ONLY do this if there are more than 3 such people DB::startTransaction(); DB::query( "UPDATE accounts SET password=%s WHERE password=%s" , 'sdfwsert4rt' , 'hello' ); $counter = DB::affectedRows(); if ( $counter > 3) { echo $counter . " people just got their password changed!!\n" ; DB::commit(); } else { echo "No one got their password changed!\n" ; DB::rollback(); } |
1 2 3 4 5 6 | $current_db_tables = DB::tableList(); $other_db_tables = DB::tableList( 'other_db' ); foreach ( $other_db_tables as $table ) { echo "Table Name: $table\n" ; } |
1 2 3 4 5 | $columns = DB::columnList( 'accounts' ); foreach ( $columns as $column ) { echo "Column: $column\n" ; } |
1 | DB::disconnect(); // drop mysqli connection |
1 | $mysqli = DB::get(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | $where = new WhereClause( 'and' ); // create a WHERE statement of pieces joined by ANDs $where ->add( 'username=%s' , 'Joe' ); $where ->add( 'password=%s' , 'mypass' ); // SELECT * FROM accounts WHERE (`username`='Joe') AND (`password`='mypass') $results = DB::query( "SELECT * FROM accounts WHERE %l" , $where ); $subclause = $where ->addClause( 'or' ); // add a sub-clause with ORs $subclause ->add( 'age=%i' , 15); $subclause ->add( 'age=%i' , 18); $subclause ->negateLast(); // negate the last thing added (age=18) // SELECT * FROM accounts WHERE (`username`='Joe') AND (`password`='mypass') AND ((`age`=15) OR (NOT(`age`=18))) $results = DB::query( "SELECT * FROM accounts WHERE %l" , $where ); $subclause ->negate(); // negate this entire subclause // SELECT * FROM accounts WHERE (`username`='Joe') AND (`password`='mypass') AND (NOT((`age`=15) OR (NOT(`age`=18)))) $results = DB::query( "SELECT * FROM accounts WHERE %l" , $where ); |
1 2 3 4 5 6 7 8 | DB:: $user = 'my_database_user' ; // configure MeekroDB like normal DB:: $password = 'my_database_password' ; DB:: $dbName = 'my_database_name' ; // ... (code passes) $mdb = new MeekroDB(); // don't need to pass any config parameters // it'll just read them from the above |
1 | $mdb = new MeekroDB( $host , $user , $pass , $dbName , $port , $encoding ); |
1 2 3 4 5 6 | $row = $mdb ->queryFirstRow( "SELECT name, age FROM tbl WHERE name=%s LIMIT 1" , 'Joe' ); echo "Name: " . $row [ 'name' ] . "\n" ; // will be Joe, obviously $mdb ->param_char = '##' ; $row2 = $mdb ->queryFirstRow( "SELECT name, age FROM tbl WHERE name=##s LIMIT 1" , 'Frank' ); echo "Name: " . $row2 [ 'name' ] . "\n" ; // will be Frank, obviously |
1 2 3 4 5 6 7 8 | $users = DB::query( "SELECT name, age, address FROM users" ); $names = DBHelper::verticalSlice( $users , 'name' ); // Above line is equivalent to: foreach ( $users as $user ) { $names [] = $user [ 'name' ]; } |
1 2 3 4 5 6 7 8 9 | $users = DB::query( "SELECT name, age, address FROM users" ); $ages = DBHelper::verticalSlice( $users , 'age' , 'name' ); // Above line is equivalent to: $names = array (); foreach ( $users as $user ) { $names [ $user [ 'name' ]] = $user [ 'age' ]; } |
1 2 3 4 5 | $users = DB::query( "SELECT name, age, address FROM users" ); $users_by_name = DBHelper::reIndex( $users , 'name' ); $frank = $users_by_name [ 'Frank' ] echo "Frank's age is " . $frank ['age'] . "\n" ; // 15 |
1 2 3 4 5 6 | $users = DB::query( "SELECT name, age, address FROM users" ); $users_by_name_and_age = DBHelper::reIndex( $users , 'name' , 'age' ); $frank = $users_by_name_and_age [ 'Frank' ][ '15' ]; echo "Frank's address is " . $frank ['address'] . "\n" ; } |
Copyright (C) 2008-2016 :: Email me :: LGPL v3 :: GitHub Tracker