Commit 22b45157 authored by Alexander Butenko's avatar Alexander Butenko

Merge pull request #271 from avbdr/master

fixes
parents cdd185b5 493c1886
......@@ -24,7 +24,7 @@ class MysqliDb
*
* @var string
*/
public static $prefix;
public static $prefix = '';
/**
* MySQLi instance
*
......@@ -167,12 +167,8 @@ class MysqliDb
$this->isSubQuery = true;
return;
}
// for subqueries we do not need database connection and redefine root instance
if (!is_object ($host))
$this->connect();
$this->setPrefix();
if (isset ($prefix))
$this->setPrefix ($prefix);
self::$_instance = $this;
}
......@@ -194,6 +190,17 @@ class MysqliDb
if ($this->charset)
$this->_mysqli->set_charset ($this->charset);
}
/**
* A method to get mysqli object or create it in case needed
*/
public function mysqli ()
{
if (!$this->_mysqli)
$this->connect();
return $this->_mysqli;
}
/**
* A method of returning the static instance to allow access to the
* instantiated object from within another class.
......@@ -643,7 +650,7 @@ class MysqliDb
*/
public function getInsertId()
{
return $this->_mysqli->insert_id;
return $this->mysqli()->insert_id;
}
/**
......@@ -655,7 +662,7 @@ class MysqliDb
*/
public function escape($str)
{
return $this->_mysqli->real_escape_string($str);
return $this->mysqli()->real_escape_string($str);
}
/**
......@@ -667,7 +674,7 @@ class MysqliDb
* @return bool True if connection is up
*/
public function ping() {
return $this->_mysqli->ping();
return $this->mysqli()->ping();
}
/**
......@@ -876,11 +883,11 @@ class MysqliDb
array_push($results, $x);
}
// stored procedures sometimes can return more then 1 resultset
if ($this->_mysqli->more_results())
$this->_mysqli->next_result();
if ($this->mysqli()->more_results())
$this->mysqli()->next_result();
if (in_array ('SQL_CALC_FOUND_ROWS', $this->_queryOptions)) {
$stmt = $this->_mysqli->query ('SELECT FOUND_ROWS()');
$stmt = $this->mysqli()->query ('SELECT FOUND_ROWS()');
$totalCount = $stmt->fetch_row();
$this->totalCount = $totalCount[0];
}
......@@ -1074,8 +1081,8 @@ class MysqliDb
*/
protected function _prepareQuery()
{
if (!$stmt = $this->_mysqli->prepare($this->_query)) {
trigger_error("Problem preparing query ($this->_query) " . $this->_mysqli->error, E_USER_ERROR);
if (!$stmt = $this->mysqli()->prepare($this->_query)) {
trigger_error("Problem preparing query ($this->_query) " . $this->mysqli()->error, E_USER_ERROR);
}
if ($this->traceEnabled)
$this->traceStartQ = microtime (true);
......@@ -1151,7 +1158,9 @@ class MysqliDb
* @return string
*/
public function getLastError () {
return trim ($this->_stmtError . " " . $this->_mysqli->error);
if (!$this->_mysqli)
return "mysqli is null";
return trim ($this->_stmtError . " " . $this->mysqli()->error);
}
/**
......@@ -1276,7 +1285,7 @@ class MysqliDb
* @uses register_shutdown_function(array($this, "_transaction_shutdown_check"))
*/
public function startTransaction () {
$this->_mysqli->autocommit (false);
$this->mysqli()->autocommit (false);
$this->_transaction_in_progress = true;
register_shutdown_function (array ($this, "_transaction_status_check"));
}
......@@ -1288,9 +1297,9 @@ class MysqliDb
* @uses mysqli->autocommit(true);
*/
public function commit () {
$this->_mysqli->commit ();
$this->mysqli()->commit ();
$this->_transaction_in_progress = false;
$this->_mysqli->autocommit (true);
$this->mysqli()->autocommit (true);
}
/**
......@@ -1300,9 +1309,9 @@ class MysqliDb
* @uses mysqli->autocommit(true);
*/
public function rollback () {
$this->_mysqli->rollback ();
$this->mysqli()->rollback ();
$this->_transaction_in_progress = false;
$this->_mysqli->autocommit (true);
$this->mysqli()->autocommit (true);
}
/**
......
......@@ -2,6 +2,7 @@
require_once ("../MysqliDb.php");
error_reporting(E_ALL);
$prefix = 't_';
$db = new Mysqlidb('localhost', 'root', '', 'testdb');
if(!$db) die("Database error");
......@@ -12,13 +13,11 @@ $db = new Mysqlidb(Array (
'host' => 'localhost',
'username' => 'root',
'password' => '',
'db'=> 'testdb',
'db' => 'testdb',
'prefix' => $prefix,
'charset' => null));
if(!$db) die("Database error");
$prefix = 't_';
$db->setPrefix($prefix);
$db->setTrace(true);
$tables = Array (
......@@ -112,6 +111,11 @@ foreach ($tables as $name => $fields) {
createTable ($prefix.$name, $fields);
}
if (!$db->ping()) {
echo "db is not up";
exit;
}
// insert test with autoincrement
foreach ($data as $name => $datas) {
foreach ($datas as $d) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment