2013-09-18 01:38:36 +02:00
|
|
|
<?php
|
2017-01-03 03:22:48 +01:00
|
|
|
/** @var mysqli $sql_connection */
|
|
|
|
$sql_connection = null;
|
2013-09-18 01:38:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Close connection.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_close()
|
|
|
|
{
|
|
|
|
global $sql_connection;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return $sql_connection->close();
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
2014-12-07 17:34:29 +01:00
|
|
|
/**
|
|
|
|
* Return NULL if given value is null.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @return bool
|
2014-12-07 17:34:29 +01:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_null($value = null)
|
|
|
|
{
|
|
|
|
return $value == null ? 'NULL' : ("'" . sql_escape($value) . "'");
|
2014-12-07 17:34:29 +01:00
|
|
|
}
|
|
|
|
|
2013-09-18 01:38:36 +02:00
|
|
|
/**
|
|
|
|
* Start new transaction.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return mysqli_result|bool
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_transaction_start()
|
|
|
|
{
|
|
|
|
global $sql_nested_transaction_level;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
if ($sql_nested_transaction_level++ == 0) {
|
2017-01-03 14:12:17 +01:00
|
|
|
return sql_query('BEGIN');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return true;
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit transaction.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return mysqli_result|bool
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_transaction_commit()
|
|
|
|
{
|
|
|
|
global $sql_nested_transaction_level;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
if (--$sql_nested_transaction_level == 0) {
|
2017-01-03 14:12:17 +01:00
|
|
|
return sql_query('COMMIT');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return true;
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop transaction, revert database.
|
2017-01-03 03:22:48 +01:00
|
|
|
*
|
|
|
|
* @return mysqli_result|bool
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_transaction_rollback()
|
|
|
|
{
|
|
|
|
global $sql_nested_transaction_level;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
if (--$sql_nested_transaction_level == 0) {
|
2017-01-03 14:12:17 +01:00
|
|
|
return sql_query('ROLLBACK');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return true;
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs an sql error.
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param string $message
|
2013-09-18 01:38:36 +02:00
|
|
|
* @return false
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_error($message)
|
|
|
|
{
|
2017-01-03 03:22:48 +01:00
|
|
|
// @TODO: Bad idea..
|
2017-01-02 03:57:23 +01:00
|
|
|
sql_close();
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$message = trim($message) . "\n";
|
|
|
|
$message .= debug_string_backtrace() . "\n";
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
error_log('mysql_provider error: ' . $message);
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return false;
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connect to mysql server.
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param string $host Host
|
|
|
|
* @param string $user Username
|
|
|
|
* @param string $pass Password
|
|
|
|
* @param string $db_name DB to select
|
|
|
|
* @return mysqli|false The connection handler
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_connect($host, $user, $pass, $db_name)
|
|
|
|
{
|
|
|
|
global $sql_connection;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$sql_connection = new mysqli($host, $user, $pass, $db_name);
|
|
|
|
if ($sql_connection->connect_errno) {
|
2017-01-03 14:12:17 +01:00
|
|
|
error('Unable to connect to MySQL: ' . $sql_connection->connect_error);
|
|
|
|
return sql_error('Unable to connect to MySQL: ' . $sql_connection->connect_error);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-03 14:12:17 +01:00
|
|
|
$result = $sql_connection->query('SET CHARACTER SET utf8;');
|
2017-01-02 15:43:36 +01:00
|
|
|
if (!$result) {
|
2017-01-03 14:12:17 +01:00
|
|
|
return sql_error('Unable to set utf8 character set (' . $sql_connection->errno . ') ' . $sql_connection->error);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$result = $sql_connection->set_charset('utf8');
|
2017-01-02 15:43:36 +01:00
|
|
|
if (!$result) {
|
2017-01-03 14:12:17 +01:00
|
|
|
return sql_error('Unable to set utf8 names (' . $sql_connection->errno . ') ' . $sql_connection->error);
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
return $sql_connection;
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the selected db in current mysql-connection.
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
2017-01-03 03:22:48 +01:00
|
|
|
* @param $db_name
|
2013-09-18 01:38:36 +02:00
|
|
|
* @return bool true on success, false on error
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_select_db($db_name)
|
|
|
|
{
|
|
|
|
global $sql_connection;
|
2017-01-02 15:43:36 +01:00
|
|
|
if (!$sql_connection->select_db($db_name)) {
|
2017-01-03 14:12:17 +01:00
|
|
|
return sql_error('No database selected.');
|
2017-01-02 03:57:23 +01:00
|
|
|
}
|
|
|
|
return true;
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MySQL SELECT query
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param string $query
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return array|false Result array or false on error
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_select($query)
|
|
|
|
{
|
|
|
|
global $sql_connection;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
|
|
|
$result = $sql_connection->query($query);
|
2017-01-02 03:57:23 +01:00
|
|
|
if ($result) {
|
|
|
|
$data = [];
|
|
|
|
while ($line = $result->fetch_assoc()) {
|
|
|
|
array_push($data, $line);
|
|
|
|
}
|
|
|
|
return $data;
|
2016-09-29 11:28:42 +02:00
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-03 14:12:17 +01:00
|
|
|
return sql_error('MySQL-query error: ' . $query . ' (' . $sql_connection->errno . ') ' . $sql_connection->error);
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MySQL execute a query
|
2013-10-13 00:52:44 +02:00
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param string $query
|
2017-01-03 03:22:48 +01:00
|
|
|
* @return mysqli_result|false boolean resource or false on error
|
2013-09-18 01:38:36 +02:00
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_query($query)
|
|
|
|
{
|
|
|
|
global $sql_connection;
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
$result = $sql_connection->query($query);
|
|
|
|
if ($result) {
|
|
|
|
return $result;
|
|
|
|
}
|
2017-01-02 15:43:36 +01:00
|
|
|
|
2017-01-03 14:12:17 +01:00
|
|
|
return sql_error('MySQL-query error: ' . $query . ' (' . $sql_connection->errno . ') ' . $sql_connection->error);
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns last inserted id.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_id()
|
|
|
|
{
|
|
|
|
global $sql_connection;
|
|
|
|
return $sql_connection->insert_id;
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Escape a string for a sql query.
|
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param string $query
|
2013-09-18 01:38:36 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_escape($query)
|
|
|
|
{
|
|
|
|
global $sql_connection;
|
|
|
|
return $sql_connection->real_escape_string($query);
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
2014-12-28 13:44:56 +01:00
|
|
|
/**
|
|
|
|
* Convert a boolean for mysql-queries.
|
2015-08-26 14:51:00 +02:00
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param boolean $boolean
|
2014-12-28 13:44:56 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_bool($boolean)
|
|
|
|
{
|
|
|
|
return $boolean == true ? 'TRUE' : 'FALSE';
|
2014-12-28 13:44:56 +01:00
|
|
|
}
|
|
|
|
|
2013-09-18 01:38:36 +02:00
|
|
|
/**
|
|
|
|
* Count query result lines.
|
|
|
|
*
|
2017-01-02 03:57:23 +01:00
|
|
|
* @param string $query
|
2013-09-18 01:38:36 +02:00
|
|
|
* @return int Count of result lines
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_num_query($query)
|
|
|
|
{
|
|
|
|
return sql_query($query)->num_rows;
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_select_single_col($query)
|
|
|
|
{
|
|
|
|
$result = sql_select($query);
|
|
|
|
return array_map('array_shift', $result);
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 03:22:48 +01:00
|
|
|
/**
|
|
|
|
* @param string $query
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2017-01-02 03:57:23 +01:00
|
|
|
function sql_select_single_cell($query)
|
|
|
|
{
|
2017-01-03 03:22:48 +01:00
|
|
|
$result = sql_select($query);
|
|
|
|
if ($result == false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array_shift($result);
|
|
|
|
if (!is_array($result)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_shift($result);
|
2013-09-18 01:38:36 +02:00
|
|
|
}
|