You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
engelsystem/includes/mysqli_provider.php

251 lines
4.8 KiB
PHTML

<?php
/** @var mysqli $sql_connection */
$sql_connection = null;
/**
* Close connection.
*
* @return bool
*/
8 years ago
function sql_close()
{
global $sql_connection;
8 years ago
8 years ago
return $sql_connection->close();
}
/**
* Return NULL if given value is null.
*
* @param mixed $value
* @return bool
*/
8 years ago
function sql_null($value = null)
{
return $value == null ? 'NULL' : ("'" . sql_escape($value) . "'");
}
/**
* Start new transaction.
*
* @return mysqli_result|bool
*/
8 years ago
function sql_transaction_start()
{
global $sql_nested_transaction_level;
8 years ago
if ($sql_nested_transaction_level++ == 0) {
return sql_query('BEGIN');
8 years ago
}
8 years ago
8 years ago
return true;
}
/**
* Commit transaction.
*
* @return mysqli_result|bool
*/
8 years ago
function sql_transaction_commit()
{
global $sql_nested_transaction_level;
8 years ago
if (--$sql_nested_transaction_level == 0) {
return sql_query('COMMIT');
8 years ago
}
8 years ago
8 years ago
return true;
}
/**
* Stop transaction, revert database.
*
* @return mysqli_result|bool
*/
8 years ago
function sql_transaction_rollback()
{
global $sql_nested_transaction_level;
8 years ago
if (--$sql_nested_transaction_level == 0) {
return sql_query('ROLLBACK');
8 years ago
}
8 years ago
8 years ago
return true;
}
/**
* Logs an sql error.
11 years ago
*
8 years ago
* @param string $message
* @return false
*/
8 years ago
function sql_error($message)
{
// @TODO: Bad idea..
8 years ago
sql_close();
8 years ago
8 years ago
$message = trim($message) . "\n";
$message .= debug_string_backtrace() . "\n";
8 years ago
8 years ago
error_log('mysql_provider error: ' . $message);
8 years ago
8 years ago
return false;
}
/**
* Connect to mysql server.
11 years ago
*
* @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
*/
8 years ago
function sql_connect($host, $user, $pass, $db_name)
{
global $sql_connection;
8 years ago
8 years ago
$sql_connection = new mysqli($host, $user, $pass, $db_name);
if ($sql_connection->connect_errno) {
error('Unable to connect to MySQL: ' . $sql_connection->connect_error);
return sql_error('Unable to connect to MySQL: ' . $sql_connection->connect_error);
8 years ago
}
8 years ago
$result = $sql_connection->query('SET CHARACTER SET utf8;');
8 years ago
if (!$result) {
return sql_error('Unable to set utf8 character set (' . $sql_connection->errno . ') ' . $sql_connection->error);
8 years ago
}
8 years ago
8 years ago
$result = $sql_connection->set_charset('utf8');
8 years ago
if (!$result) {
return sql_error('Unable to set utf8 names (' . $sql_connection->errno . ') ' . $sql_connection->error);
8 years ago
}
8 years ago
8 years ago
return $sql_connection;
}
/**
* Change the selected db in current mysql-connection.
11 years ago
*
* @param $db_name
* @return bool true on success, false on error
*/
8 years ago
function sql_select_db($db_name)
{
global $sql_connection;
8 years ago
if (!$sql_connection->select_db($db_name)) {
return sql_error('No database selected.');
8 years ago
}
return true;
}
/**
* MySQL SELECT query
11 years ago
*
8 years ago
* @param string $query
* @return array|false Result array or false on error
*/
8 years ago
function sql_select($query)
{
global $sql_connection;
8 years ago
$result = $sql_connection->query($query);
8 years ago
if ($result) {
$data = [];
while ($line = $result->fetch_assoc()) {
array_push($data, $line);
}
return $data;
}
8 years ago
return sql_error('MySQL-query error: ' . $query . ' (' . $sql_connection->errno . ') ' . $sql_connection->error);
}
/**
* MySQL execute a query
11 years ago
*
8 years ago
* @param string $query
* @return mysqli_result|false boolean resource or false on error
*/
8 years ago
function sql_query($query)
{
global $sql_connection;
8 years ago
8 years ago
$result = $sql_connection->query($query);
if ($result) {
return $result;
}
8 years ago
return sql_error('MySQL-query error: ' . $query . ' (' . $sql_connection->errno . ') ' . $sql_connection->error);
}
/**
* Returns last inserted id.
*
* @return int
*/
8 years ago
function sql_id()
{
global $sql_connection;
return $sql_connection->insert_id;
}
/**
* Escape a string for a sql query.
*
8 years ago
* @param string $query
* @return string
*/
8 years ago
function sql_escape($query)
{
global $sql_connection;
return $sql_connection->real_escape_string($query);
}
/**
* Convert a boolean for mysql-queries.
*
8 years ago
* @param boolean $boolean
* @return string
*/
8 years ago
function sql_bool($boolean)
{
return $boolean == true ? 'TRUE' : 'FALSE';
}
/**
* Count query result lines.
*
8 years ago
* @param string $query
* @return int Count of result lines
*/
8 years ago
function sql_num_query($query)
{
return sql_query($query)->num_rows;
}
8 years ago
function sql_select_single_col($query)
{
$result = sql_select($query);
return array_map('array_shift', $result);
}
/**
* @param string $query
* @return string|null
*/
8 years ago
function sql_select_single_cell($query)
{
$result = sql_select($query);
if ($result == false) {
return null;
}
$result = array_shift($result);
if (!is_array($result)) {
return null;
}
return array_shift($result);
}