Skip to content

Instantly share code, notes, and snippets.

@Lathanao
Last active November 22, 2023 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lathanao/638fd027366487b1f6c4b8947486ed5f to your computer and use it in GitHub Desktop.
Save Lathanao/638fd027366487b1f6c4b8947486ed5f to your computer and use it in GitHub Desktop.
mysql bootsrap class for php
<?php
header('Content-type: text/html; charset=utf-8');
// header('Content-type: text/plain; charset=utf-8');
ini_set('display_errors', 1);
ini_set('track_errors', 1);
ini_set('html_errors', 1);
ini_set('realpath_cache_size', '5M');
ini_set('max_execution_time', 9999999900000);
error_reporting(E_ALL);
define('_USER_', 'username');
define('_PASSWD_', 'password');
define('_DB_', 'database');
global $mysqli;
$mysqli = new mysqli('localhost', _USER_, _PASSWD_, _DB_);
$timeStart = microtime(true);
$mysqli->query('SELECT * FROM schema.table');
$timeEnd = microtime(true);
$time = $timeEnd - $timeStart;
echo PHP_EOL . "Execution time : $time";
echo PHP_EOL . '--- END ---';
die();
/**
* Execute an array of sql for updating base
*
* @param $allSql
*
* @return bool
*/
function executeArray($allSql)
{
global $mysqli;
if ($mysqli->connect_errno) {
echo '<br>----------------------------';
echo '<br>Sorry, first try connect_errno.';
}
foreach ($allSql as $key => $sql) {
if (!$result = $mysqli->query($sql)) {
echo '<br>----------------------------';
echo '<br>Sorry, here the problem:';
echo '<br>Query: ' . $sql;
echo '<br>Errno: ' . $mysqli->errno;
echo '<br>Error: ' . $mysqli->error;
exit;
}
}
return true;
}
/**
* Execute an SQL, and return an result array
*
* @return array of int
*/
function executeS($sql)
{
global $mysqli;
if ($mysqli->connect_errno) {
echo '<br>----------------------------';
echo '<br>Sorry, first try connect_errno.';
}
if (!$result = $mysqli->query($sql)) {
echo '<br>----------------------------';
echo '<br>Sorry, here the problem:';
echo '<br>Query: ' . $sql;
echo '<br>Errno: ' . $mysqli->errno;
echo '<br>Error: ' . $mysqli->error;
exit;
}
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
/**
* Execute only one sql for the setting of base eg ALTER ...
*
* @return bool
*/
function executeOne($sql)
{
global $mysqli;
if ($mysqli->connect_errno) {
echo '<br>----------------------------';
echo '<br>Sorry, first try connect_errno.';
}
if (!$result = $mysqli->query($sql)) {
echo '<br>----------------------------';
echo '<br>Sorry, here the problem:';
echo '<br>Query: ' . $sql;
echo '<br>Errno: ' . $mysqli->errno;
echo '<br>Error: ' . $mysqli->error;
exit;
}
return true;
}
/**
* Execute an SQL, and return an result array
*
* @return array of int
*/
function getValue($sql)
{
$result = connectAndExecuteSQL($sql);
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
function connectAndExecuteSQL($sql, $is_array = null)
{
global $mysqli;
if ($mysqli->connect_errno) {
echo '<br>----------------------------';
echo '<br>Sorry, first try connect_errno.';
}
if (!$result = $mysqli->query($sql)) {
echo '<br>----------------------------';
echo '<br>Sorry, here the problem:';
echo '<br>Query: ' . $sql;
echo '<br>Errno: ' . $mysqli->errno;
echo '<br>Error: ' . $mysqli->error;
exit;
}
return $mysqli;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment