PHP code example of bentools / mysqliextended
1. Go to this page and download the library: Download bentools/mysqliextended 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/ */
bentools / mysqliextended example snippets
$cnx = new MySqliExtended();
$cnx->options(MYSQLI_OPT_CONNECT_TIMEOUT, 5);
$cnx->real_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$query = "SELECT 'Bill' AS firstname, 'Gates' AS lastname UNION SELECT 'Tim' AS firstname, 'Cook' AS lastname";
$result = $cnx->sqlArray($query);
Output:
array (
0 =>
array (
'firstname' => 'Bill',
'lastname' => 'Gates',
),
1 =>
array (
'firstname' => 'Tim',
'lastname' => 'Cook',
),
)
$result = $cnx->sqlRow($query);
Output:
array (
'firstname' => 'Bill',
'lastname' => 'Gates',
)
$result = $cnx->sqlColumn($query);
Output:
array (
0 => 'Bill',
1 => 'Tim',
)
$result = $cnx->sqlValue($query);
Output:
'Bill'
// Before :
$query = "INSERT INTO `ceo` (firstname, lastname, years_in_company) VALUES (?, ?, ?)";
$stmt = $cnx->prepare($query);
$firstname = 'Larry';
$lastname = 'Page';
$years_in_company = 12;
$stmt->bind_param('ssi', $firstname, $lastname, $years_in_company);
$stmt->execute();
// Now :
$stmt = $cnx->prepare($query);
$stmt->sql(array(
'Larry',
'Page',
12
));
// or directly :
$cnx->sql($query, array(
'Larry',
'Page',
12
));
// or very shortly :
$cnx($query, array(
'Larry',
'Page',
12
));
$query = "INSERT INTO `ceo` (firstname, lastname, years_in_company) VALUES (:firstname, :lastname, :years_in_company) ON DUPLICATE KEY UPDATE years_in_company = :years_in_company";
$cnx->sql($query, array(
'firstname' => 'Larry',
'lastname' => 'Page',
'years_in_company' => 12
));
$query = "SELECT firstname, lastname FROM `ceo` WHERE firstname LIKE ?";
$result = $cnx($query)->sqlRow('bill%');
Ouput :
array (
'firstname' => 'Bill',
'lastname' => 'Gates',
)
$result = $cnx($query)->sqlRow('larry%');
Ouput :
array (
'firstname' => 'Larry',
'lastname' => 'Page',
)
$query = "SELECT firstname, lastname FROM `ceo` WHERE (firstname LIKE :firstname OR years_in_company > :years)";
$result = $cnx($query)->sqlArray(array(
'firstname' => 'larry%',
'years' => 10
));
Output:
array (
0 =>
array (
'firstname' => 'Bill',
'lastname' => 'Gates',
),
1 =>
array (
'firstname' => 'Tim',
'lastname' => 'Cook',
),
2 =>
array (
'firstname' => 'Larry',
'lastname' => 'Page',
),
)