-
Notifications
You must be signed in to change notification settings - Fork 3
Usage
I strongly recommend reading from the beginning to the end for the first time, as you will able to know which method is perfect for you.
You can connect in 3 ways.... in your mopconfig.php, change the default driver to your preferred driver. SUPPORTED DRIVER ARE
mysqli and pdo
now connection can be made using your preferred driver
$connect = new Mysql\mop($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME)
If you like to change the connection driver on the run without editing the settings then you can do this
$connect = new Mysql\mop($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME,'mysqli');
//this will override the default setting and connect using mysqli
OR
$connect = new Mysql\mop($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME,'pdo');
//this will override the default setting and connect using pdo
NOTE It is case insensitive, if cofig file is missing or you don't specified which driver to use, default is MYSQLI
Query method is supported by both mysqli and pdo, depending on your preferred driver, connection will be made to that driver, you don't have to worry about how to connect using pdo or mysqli.
$query = "SELECT name FROM userdata";
$connect->query($query);
Your query will be prepared automatically.
This is useful if your query contain parameter to bind. you can bind in different way depending on which driver is been used.
IF CONNECTION IS MADE ON MYSQLI i.e your driver is mysqli you can bind using mysqli way of binding.
$query = "SELECT name FROM userdata WHERE ID = ?";
$connect->query($query);
$id = 4
$connect->param('i',$id)
IF CONNECTION IS MADE ON PDO i.e your driver is pdo you can bind using PDO way of binding....it has 4 ways of binding
- first way unnamed parameter
$query = "SELECT name FROM userdata WHERE ID = ?";
$connect->query($query);
$id = 4
//you bind by index according to your query
$connect->param(1,$id);
$query = "SELECT name FROM userdata WHERE ID = ? and Token = ?";
$connect->query($query);
$id = 4
$token = 'sdfsffekr';
$connect->param(1,$id);
$connect->param(2,$token);
- second way named parameter
$query = "SELECT name FROM userdata WHERE ID = :id and Token = :token";
$connect->query($query);
$id = 4
$token = 'sdfsffekr';
$connect->param(:id,$id);
$connect->param(:token,$token);
You can run any query either has a bind parameter or not.
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$query = "SELECT name FROM userdata WHERE ID = ?";
$connect->query($query);
$id = 4;
$connect->param('i',$id);
$connect->run();
$query = "SELECT name FROM userdata WHERE ID = :id";
$connect->query($query);
$id = 4
$connect->param(:id,$id);
$connect->run();
This is the 3rd way of binding in PDO, when using this method you don't need param and run method.
Unnamed parameter
$query = "SELECT name FROM userdata WHERE ID = ? and Token = ?";
$connect->query($query);
$data = array(4, 'hdjssd'); // your value must be in order of placeholder
$connect->run_all($data);
// this will run automatically and get you your result.
named parameter
$query = "SELECT name FROM userdata WHERE ID = :id and Token = :token";
$connect->query($query);
$data = array('id' => 4,'token' => 'hdjssd'); // array key must match placeholder
$connect->run_all($data);
// this will run automatically and get you your result.
This is the 4th way of binding in PDO, when using this method you don't need param and run method.
// a simple object
class data
{
public $id;
public $token;
function __construct($i, $a)
{
$this->id = $i;
$this->token = $a;
}
}
$data = new data (1,'fssd');
$connect->run_all((array)$data); //Note the array there.... It means the property are treated as an array;
You can add as many query has you like and you will get result separately.
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
//get the result
$query2 = "SELECT age FROM member";
$connect->add_query($query2);
//get the result
$query3 = "INSERT INTO data VALUES (1, 'fddd')";
$connect->add_query($query3);
But if you want to add a query that has a bind parameter you have to start from the beginning.
Bind parameter according to the default driver
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
//get the result
$query2 = "SELECT age FROM member";
$connect->add_query($query2);
//get the result
$query3 = "INSERT INTO data VALUES (?, ?)";
$connect->query($query3);
$connect->param('is', 1, 'fddd');
$connect->run();
//get the result
You can get any column of any result as long as the query return a result. It support both column name and index. NOTE
Bind parameter according to the default driver
$query = "SELECT name FROM userdata WHERE ID = :id";
$connect->query($query);
$id = 4;
$connect->param(:id,$id);
$connect->run();
$row = $connect->get_column('name'); // an array is return.
for($i=0; $i<count($row); $i++)
{
echo $row[$i];
}
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$column = $connect->get_column(0); // an array is return.
for($i=0; $i<count($column); $i++)
{
echo $column[$i];
}
close the connection and reconnect.
mostly use when database details has change.
$connect->reconnect();
You can change or connect to another database on the run...
//selecting from the previous database
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$connect->get_column('name');
//change database
$connect->change_db('database name');
//previous connection will be close and new connection will be made.
$connect->reconnect();
//You can now select from the new database you change to.
$query = "SELECT name FROM newtable";
$connect->query($query);
$connect->run();
$connect->get_column('name');
You can change or connect to another host on the run...
//selecting from the previous database
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$connect->get_column('name');
//change host
$connect->change_host('host name');
//previous connection will be close and new connection will be made.
$connect->reconnect();
//You can now select from the new host you change to.
$query = "SELECT name FROM newtable";
$connect->query($query);
$connect->run();
$connect->get_column('name');
You can change or connect to database with another password..
//selecting from the previous database with old password
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$connect->get_column('name');
//change password
$connect->change_password('password');
//previous connection will be close and new connection will be made.
$connect->reconnect();
//You can now select from the database with the new password.
$query = "SELECT name FROM newtable";
$connect->query($query);
$connect->run();
$connect->get_column('name');
You can change your username on the run and reconnect...
//selecting from the previous database
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$connect->get_column('name');
//change username
$connect->change_db('username');
//previous connection will be close and new connection will be made.
$connect->reconnect();
//You can now select using the username you change to.
$query = "SELECT name FROM newtable";
$connect->query($query);
$connect->run();
$connect->get_column('name');
You can change all database information on the run and reconnect.
//selecting from the previous database
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$connect->get_column('name');
//changing everything
$connect->change_all('host name', 'username', 'password', 'database name');
//previous connection will be close and new connection will be made.
$connect->reconnect();
//You can now select from the database using the information you provide.
$query = "SELECT name FROM newtable";
$connect->query($query);
$connect->run();
$connect->get_column('name');
Running multiple query has become easy..
supported by both MYSQLI and PDO connection
It does not support query with bind parameter.
$query = "SELECT name FROM userdata;INSERT INTO people VALUE ('name','age');SELECT age FROM people";
$connect->multi_query($query);
//You query will run automatically and result is ready
Use it when you use multiple query method to get column
First parameter is the query index.
Second parameter is the column index, support column name and index.
$query = "SELECT name FROM userdata;INSERT INTO people VALUE ('name','age');SELECT age FROM people";
$connect->multi_query($query);
// first argument is the query index you want to get it's column
$column = $connect->multi_get_column(0, 'name'); // an array is return.
for($i=0; $i<count($column); $i++)
{
echo $column[$i];
}
Get the query results as csv when you use multiple query method
First parameter is the query index.
Second parameter is the column index, support column name and index.
If no result is return from the query it will return null.
$query = "SELECT name FROM userdata;INSERT INTO people VALUE ('name','age');SELECT age FROM people";
$connect->multi_query($query);
$column = $connect->multi_csv(0); // csv string is return.
Get the query results as csv with the header row when you use multiple query method
First parameter is the query index.
Second parameter is the column index, support column name and index.
If no result is return from the query it will return null.
$query = "SELECT name FROM userdata;INSERT INTO people VALUE ('name','age');SELECT age FROM people";
$connect->multi_query($query);
$column = $connect->multi_csv_header(0); // csv string is return.
Get a statement results header row when you use multiple query method
First parameter is the query index.
If no result is return from the query it will return null.
$query = "SELECT name FROM userdata;INSERT INTO people VALUE ('name','age');SELECT age FROM people";
$connect->multi_query($query);
$column = $connect->multi_header_row(2); // array of all header row is return.
for($i=0; $i<count($column); $i++)
{
echo $column[$i];
}
Get the last insert id of any insert statement in the multi query when using multiple query method
First parameter is the query index.
If statement is not an insert statement it will return null.
$query = "SELECT name FROM userdata;INSERT INTO people VALUE ('name','age');SELECT age FROM people";
$connect->multi_query($query);
$id = $connect->multi_insert_id(1); // int is returen.
echo $id;
Get the number of affected or selected rows when using multiple query method
First parameter is the query index.
If statement is select statement and return no result number of row is zero.
If statement is anything else it will return number of affected row.
$query = "SELECT name FROM userdata;INSERT INTO people VALUE ('name','age');SELECT age FROM people";
$connect->multi_query($query);
$select = $connect->multi_num_of_rows(0); // int is returen.
echo $select;
$insert = $connect->multi_num_of_rows(1); // int is returen.
echo $insert;
Clean and clear all result so you can select from index 0 in multi query.
//selecting
$query = "SELECT name FROM userdata; INSERT INTO people VALUE ('name','age'); SELECT age FROM people"; //FIRST QUERY
$connect->multi_query($query);
$index = 2; // The first query only contain 3 statement which is 0, 1, 2... so getting the LAST statement result which is 2.
$connect->multi_get_column($index,'column name'); // will get the column from the first query
//Another statements
$query = "SELECT name FROM newtable; INSERT INTO users VALUE ('name','age'); SELECT state FROM country"; // SECOND QUERY
$connect->multi_query($query);
//If you don't free the result then you are selecting index will NOT start from 0.
$index = 5; // The second query only contain 3 statement which is 0, 1, 2... so getting the LAST statement result which is 5.
$connect->multi_get_column($index,'column name'); // will get the column from the second query
USING FREE RESULTS
//selecting
$query = "SELECT name FROM userdata; INSERT INTO people VALUE ('name','age'); SELECT age FROM people"; //FIRST QUERY
$connect->multi_query($query);
$index = 2; // The first query only contain 3 statement which is 0, 1, 2... so getting the LAST statement result which is 2.
$connect->multi_get_column($index,'column name'); // will get the column from the first query
//Another statements
$query = "SELECT name FROM newtable; INSERT INTO users VALUE ('name','age'); SELECT state FROM country"; // SECOND QUERY
$connect->multi_query($query);
// TO start from index 0 then free the result.
$connect->free_results();
$index = 2; // The second query only contain 3 statement which is 0, 1, 2... so getting the LAST statement result which is 2.
$connect->multi_get_column($index,'column name'); // will get the column from the second query
Change the state of log warning on the run
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$column = $connect->get_column(0); // an array is return.
for($i=0, $i<lenght($column), $i++)
{
echo $column[$i];
}
$connect->log_warning(true); // log the warning of next query
$query2 = "SELECT age FROM people";
$connect->add_query($query2);
// If there is any warning from mysql then you can check if there is.
warning...warning error number... warning message... warning sqlstate number of warning
Change the state of displaying of error on the run
// FATAL ERROR WILL OCCUR
$query = "SELECT name FROM invalid_table";
$connect->query($query);
$connect->run();
$connect->display_error(false);
// ERROR WILL REMAIN HIDDEN UNTIL YOU CHECK
$query = "SELECT name FROM invalid_table";
$connect->query($query);
$connect->run();
$column = $connect->get_column(0); // an array is return.
for($i=0, $i<lenght($column), $i++)
{
echo $column[$i];
}
Change the driver use on the run on the run
$query = "SELECT `name` FROM `table` WHERE id = ?";
$connect->query($query);
$id = 3;
//using MYSQLI method of biding
$connect->param('s',$id);
$connect->run();
$connect->driver('pdo');
$query = "SELECT `name` FROM `table` WHERE id = ?";
$connect->query($query);
$id = 3;
//using PDO method of biding
$connect->param(1,$id);
$connect->run();
$column = $connect->get_column(0); // an array is return.
for($i=0, $i<lenght($column), $i++)
{
echo $column[$i];
}
close the database connection when you are done...it is the same for both mysqli or pdo
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$column = $connect->get_column(0); // an array is return.
for($i=0; $i<lenght($column); $i++)
{
echo $column[$i];
}
$connect->close(); //close the connection.
Get all column as csv
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$column = $connect->csv; //csv string is return
echo $column;
$connect->close(); //close the connection.
import csv file or string
// parameter order
// IS STRING => boolean -- specify if your provided csv is a string or file
// CSV => string -- your csv file or string
// TABLE NAME => string -- your table name you want to import to
// COLUMN ORDER => array -- ( optional ) if the data in each row of your csv is not in the same order as in your table then list the corresponding column name in an array.
// HAS HEADER => boolean -- ( optional ) specify if your csv has the header row or not. ( DEFAULT = false ).
$order = array('id', 'name');
$connect->import_csv(false, 'path/to/csv/file.csv', 'my_table', $order, true);
$connect->close(); //close the connection.
YOU CAN USE NULL TO SKIP ANY OPTIONAL PARAMETER
set the settings to use to import
// parameter order
// INSERT TYPE => string -- ( optional ) specify the type if duplicate key is found ( options = UPDATE, IGNORE, REPLACE ) -- DEFAULT = UPDATE
// SEPERATOR => string -- ( optional ) specify the seperator used in your csv
// ENCLOSURE => string -- ( optional ) specify the character used to enclose each column
$connect->import_csv_settings('update', ',', '"');
$connect->close(); //close the connection.
YOU CAN USE NULL TO SKIP ANY OPTIONAL PARAMETER
INSERT TYPE EXPLAIN
UPDATE
On duplicate key, it update the row with the new one provided in your csv
IGNORE
On duplicate key, it ignore the new data provided in your csv and keep the one already in the database
REPALCE
On duplicate key, it delete the row in your table and insert new row
get how many rows is inserted
$row = $connect->import_csv_num_of_rows;
echo $row;
$connect->close(); //close the connection.
Get all statemnt column as csv
$query = "SELECT name FROM userdata;SELECT age FROM people";
$connect->multi_query($query);
$columns = $connect->multi_csv; // an array of csv string is return
for($i=0; $i<count($columns); $i++)
{
echo $columns[$i]
echo '------------------'; // echo a divider
}
$connect->close(); //close the connection.
Get all column as csv with the header row
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$column = $connect->csv_header; //csv string is return
echo $column;
$connect->close(); //close the connection.
Get all statemnt column as csv with their header row
$query = "SELECT name FROM userdata;SELECT age FROM people";
$connect->multi_query($query);
$columns = $connect->multi_csv_header; // an array is return
for($i=0; $i<count($columns); $i++)
{
echo $columns[$i]
echo '------------------'; // echo a divider
}
$connect->close(); //close the connection.
Get only the header row
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$row = $connect->header_row; //string is return
echo $row;
$connect->close(); //close the connection.
Get all statemnt header row
$query = "SELECT name FROM userdata;SELECT age FROM people";
$connect->multi_query($query);
$columns = $connect->multi_header_row; // an array is return
for($i=0; $i<count($columns); $i++)
{
echo $columns[$i]
echo '------------------'; // echo a divider
}
$connect->close(); //close the connection.
You can check for error at at time, there is no different property for checking different error just one property. that makes it easy to use.
if display error is set to true an error that occur will result to fatal error
If display error is set to false but you try to get column that does not exist. a fatal error will still occur.
$connect = new Mysql\mop($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME);
if($connect->error)
{
// there is an error
}
else
{
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$row = $connect->get_column(3); //array is return
echo $row;
$connect->close(); //close the connection.
}
This check for error at the middle of query
$connect = new mysql\mop($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME);
if($connect->error)
{
// there is an error
}
else
{
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
if($connect->error)
{
// there is an error
}
else
{
$row = $connect->get_column(3); //array is return
echo $row;
$connect->close(); //close the connection.
}
}
show error message if there is
If there is an error, any other method or property will not get execute because of the previous error
$connect = new mysql\mop($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME);
if($connect->error)
{
$error = $connect->error_message; //get the error message
echo $error;
}
else
{
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
if($connect->error)
{
$error = $connect->error_message; // get the error message.
echo $error;
}
else
{
$row = $connect->get_column(3); //array is return
echo $row;
$connect->close(); //close the connection.
}
}
Check and get mysql warning
It only support by mysqli
Log warning must be set to true to see any warning. or on the run Log warning
you can check for warning of a query at any time.
$connect = new mysql\mop($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME);
if($connect->error)
{
$error = $connect->error_message; //get the error message
echo $error;
}
else
{
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
if($connect->error)
{
$error = $connect->error_message; // get the error message.
echo $error;
}
else
{
$row = $connect->get_column(3); //array is return
echo $row;
if($connect->warning) //check if there is warning
{
$count = $connect->num_of_warnings; //see how many warning occur
for($i=0,$i<$count,$i++)
{
echo $connect->warning_message[$i]; //get the warning message
echo $connect->warning_errno[$i]; //get the warning error number
echo $connect->warning_sqlstate[$i]; //get the warning sql state.
}
}
$connect->close(); //close the connection.
}
}
check if a result return a row
$connect = new mysql\mop($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME);
if($connect->error)
{
$error = $connect->error_message; //get the error message
echo $error;
}
else
{
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
if($connect->error)
{
$error = $connect->error_message; // get the error message.
echo $error;
}
else
{
if($connect->num_of_rows > 0) //check if number of rows is more than zero
{
$row = $connect->get_column(3); //array is return
}
$connect->close(); //close the connection.
}
}
Get last insert id of insert statement
$query = "INSERT INTO userdata VALUES ('name','place')";
$connect->query($query);
$connect->run();
$id = $connect->insert_id; //int is return
echo $id;
$connect->close(); //close the connection.
You can run your query your way at any where
You must perform your query according to the driver.
$connect = new mysql\mop($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME);
$query = "SELECT name FROM userdata";
$connect->query($query);
$connect->run();
$column = $connect->get_column(3); //array is return
$connect->close(); //close the connection
// MY WAY .......
$myway = $connect->connect;
$myquery = "SELECT .....";
// perform the normal way of perfoming query as if this library is not use
$mysqli = $myway->prepare($query);
$mysqli->excute();
// then get your result your way.
// also remember to close the connection your way
Also can be use for mysqli real escape string
$connect = new mysql\mop($DB_ADDRESS,$DB_USER,$DB_PASS,$DB_NAME);
$myway = $connect->connect;
$data = mysqli_real_escape_string($myway, $_POST['data']);
LICENSE
Fork and feel free to pull request......
MOP is licensed under the MIT License.
π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬ π³π¬