<?php if(!defined('SYSTEM_KEY')) die('This file is not success');
class Database
{
public static $server_connect = null;
protected static $query;
public static function server_connect()
{
if(empty(self::$server_connect))
{
self::$server_connect = new mysqli( Zzika::config('hostname', 'mysqli'),
Zzika::config('username', 'mysqli'),
Zzika::config('password', 'mysqli'),
Zzika::config('database', 'mysqli'),
Zzika::config('port', 'mysqli'),
Zzika::config('charset', 'mysqli'));
if(mysqli_connect_errno())
{
self::$server_connect = null;
Error::err_print('Произошла MySQLi ошибка при соеденении с сервером');
}
}
}
public static function query($query_string)
{
self::server_connect();
$server = self::$server_connect;
$query = self::$query;
$server = self::$server_connect;
$query = self::$query;
$query = $server->query($query_string, Zzika::config('buffering_type', 'mysqli'));
if($query == true)
{
return $query;
}
else
{
$error = $server->error;
$trace = debug_backtrace();
$error_log = date("Y-m-d h:i:s") .' '. $error .'
<b>Query: </b><br>
<pre><span style="color:#CC0000">'. $trace[0]['args'][0] .'</pre></span><br><br>
<b>File: </b><b style="color:#660099">'. $trace[0]['file'] .'</b><br>
<b>Line: </b><b style="color:#660099">'. $trace[0]['line'] .'</b>';
file_put_contents(APLICATION_DIR .'logs/mysqli.log', strip_tags($error_log) ."\n\n", FILE_APPEND);
Error::err_print('Произошла MySQLi ошибка');
}
}
public static function EscateString($string)
{
if(empty(self::$server_connect))
{
self::server_connect();
}
return self::$server_connect->real_escape_string($string);
}
public static function num_rows()
{
return self::$query->num_rows;
}
public static function fetch_assoc()
{
return self::$query->fetch_assoc();
}
public static function query_close()
{
self::$query->close();
}
}
Мне например нужно подсчитать количество строк в БД - делаю так:
$queryForCount = Database::query("SELECT * FROM `".$nameTable."` WHERE `id`='".(int)$catId."'");
$count_rows = Database::num_rows();
мне пишет типа - Notice: Trying to get property of non-object in Z:\home\mvc\www\aplication\modules\database\init.php on line 72
линия 72 это:
/...
return self::$query->num_rows;
/..