PHP code example of madbyad / mpl-mysql

1. Go to this page and download the library: Download madbyad/mpl-mysql library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

madbyad / mpl-mysql example snippets


// establish a new connection
$mysql = new MySQL("localhost", "root", "", "my_database");

// return the mysql connection
$mysql->getConnection();

// establish a default connection
MySQL::setDefaultConnection("localhost", "root", "", "my_database");

// return the default connection
MySQL::getDefaultConnection();

// prepare a new query
// no connection is supplied so it will use the default connection
$query = new MySQLQuery("SELECT * FROM user WHERE name LIKE ?");

// Establish a mysql connection
$mysql = new MySQL("localhost", "root", "", "my_database");

// prepare a new query
// use the given connection
$query = new MySQLQuery("SELECT * FROM user WHERE name LIKE ?", $mysql);

// prepare a new query
// no connection is supplied so it will use the default connection
$query = new MySQLQuery("SELECT * FROM user WHERE name LIKE ?");

// bind values into the query
$query->bind($name);

// prepare a new query
// no connection is supplied so it will use the default connection
$query = new MySQLQuery("SELECT * FROM user WHERE name LIKE ?");

// bind values into the query
$query->bind($name);

// execute the query
$query->execute();

// prepare a new query
// no connection is supplied so it will use the default connection
$query = new MySQLQuery("SELECT * FROM user WHERE name LIKE ?");

// bind values into the query
$query->bind($name);

// execute the query
$query->execute();

// return the result since it is a SELECT query
$result = $query->result();

// create a new user
MySQLCRUD::create("user", ["name", "password"], [$name, $password]);

// read from the table user and read all column
MySQLCRUD::read("user");

// read from the table user
// and only read the name and description column
MySQLCRUD::read("user", ["name", "description"]);

// read from the table user
// but only read if the name and password match the given value
MySQLCRUD::read("user", [], ["name = ?", "password = ?"], [$name, $password]);

// read from the table user
// limit the readed rows by 10
// offset the starting rows to read by 10
MySQLCRUD::read("user", [], [], [], [
    "limit" => 10,
    "offset" => 10,
]);

// read from the table user
// order the return rows by name ascendingly (A to Z)
MySQLCRUD::read("user", [], [], [], [
    "orderBy" => "name",
    "orderType" => "ascending",
]);

// read from the table user
// order the return rows by name descendingly (Z to A)
MySQLCRUD::read("user", [], [], [], [
    "orderBy" => "name",
    "orderType" => "ascending",
]);

// Update the user description who has the given name and id
MySQLCRUD::update(
    "user",
    ["description = ?"],
    [$description],
    ["name = ?", "id = ?"],
    [$name, $id]
);

// delete a user who has the given name and id
MySQLCRUD::delete("user", ["name = ?", "id = ?"], [$name, $id]);

// WARNING
// if no condition is supplied this will delete all data
MySQLCRUD::delete("user");

// construct the class
$query = new MySQLBuilder;

// insert a new user
$query->queryInsert("user", ["name", "password"], [$name, $password]);

// construct the class
$query = new MySQLBuilder;

// select from the table user and grab all column
$query->querySelect("user", []);

// construct the class
$query = new MySQLBuilder;

// select from the table user and grab the name and description column
$query->querySelect("user", ["name", "description"]);

// construct the class
$query = new MySQLBuilder;

// * select from the table user and grab the name and description column
// * then executed the query
$query->querySelect("user", ["name", "description"])
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * select from the table post
// * join the table user
//   based on the column owner on the post table
//   and the column name on the user table
// * then executed the query
$query->querySelect("post", [])
    ->join("user", [], "owner", "name")
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * select from the table post
// * only select if the name and id match the given name and id
// * then executed the query
$query->querySelect("user", [])
    ->condition(["name = ?", "id = ?"], [$name, $id])
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * select from the table post
// * only select if the name match the name A or name B or Name C
// * then executed the query
$query->querySelect("user", [])
    ->condition(["name = ?"], [$nameA])
    ->condition(["name = ?"], [$nameB])
    ->condition(["name = ?"], [$nameC])
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * select from the table post
// * only select if the name match the given name or the given id
// * then executed the query
$query->querySelect("user", [])
    ->condition(["name = ?"], [$name])
    ->condition(["id = ?"], [$id])
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * select from the table post
// * only select if the name match the given name
// * limit the selection by 10
// * then executed the query
$query->querySelect("user", [])
    ->condition(["name = ?"], [$name])
    ->limit(10);
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * select from the table post
// * only select if the name match the given name
// * offset the selection by 10
// * then executed the query
$query->querySelect("user", [])
    ->condition(["name = ?"], [$name])
    ->offset(10);
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * select from the table post
// * limit the selection by 10
// * offset the selection by 10
// * then executed the query
$query->querySelect("user", [])
    ->limit(10);
    ->offset(10);
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * select from the table post
// * order the selection by name column ascendingly A to Z
// * then executed the query
$query->querySelect("user", [])
    ->orderBy("name")
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * select from the table post
// * order the selection by name column descendingly Z to A
// * then executed the query
$query->querySelect("user", [])
    ->orderBy("name", false)
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * update the user table
$query->queryUpdate("user");

// construct the class
$query = new MySQLBuilder;

// * update the user table
// * update the column name to be the given new name
// * update the column picture to be the given new picture
$query->queryUpdate("user")
    ->set("name", $name);
    ->set("picture", $picture);

// construct the class
$query = new MySQLBuilder;

// * update the user table
// * update the column name to be the given new name
// * update the column picture to be the given new picture
// * then executed the query
$query->querySelect("user")
    ->set("name", $name);
    ->set("picture", $picture);
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * update the user table
// * update the column name to be the given new name
// * update the column picture to be the given new picture
// * update only for the user who has the given id
// * then executed the query
$query->querySelect("user")
    ->set("name", $name);
    ->set("picture", $picture);
    ->condition(["id = ?"], $id)
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * delete data from the user table
$query->queryDelete("user");

// construct the class
$query = new MySQLBuilder;

// * delete data from the user table
// * then executed the query
$query->queryDelete("user")
    ->execute();

// construct the class
$query = new MySQLBuilder;

// * delete data from the user table
// * only delete data that has the same id as the given id
// * then executed the query
$query->querySelect("user")
    ->condition(["id = ?"], $id)
    ->execute();