From bc6ddffc4681cfdaf94c5b137023ab78c142ae0f Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 12 Aug 2015 21:05:33 +0000 Subject: [PATCH 1/2] bugfix do not add table prefix if table name contains a dot --- MysqliDb.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/MysqliDb.php b/MysqliDb.php index 75eb0768..a7f2c08e 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -443,7 +443,11 @@ public function get($tableName, $numRows = null, $columns = '*') $columns = '*'; $column = is_array($columns) ? implode(', ', $columns) : $columns; - $this->_tableName = self::$prefix . $tableName; + if (strpos ($tableName, '.') === false) + $this->_tableName = self::$prefix . $tableName; + else + $this->_tableName = $tableName; + $this->_query = 'SELECT ' . implode(' ', $this->_queryOptions) . ' ' . $column . " FROM " . $this->_tableName; $stmt = $this->_buildQuery($numRows); From 9bbea7a2ee5e372ddd3067e3bcc07ae381ee12f9 Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Wed, 12 Aug 2015 21:10:11 +0000 Subject: [PATCH 2/2] Added tableExists --- MysqliDb.php | 20 ++++++++++++++++++++ readme.md | 9 +++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/MysqliDb.php b/MysqliDb.php index a7f2c08e..4c722a82 100644 --- a/MysqliDb.php +++ b/MysqliDb.php @@ -1457,5 +1457,25 @@ private function _traceGetCaller () { return __CLASS__ . "->" . $caller["function"] . "() >> file \"" . str_replace ($this->traceStripPrefix, '', $caller["file"] ) . "\" line #" . $caller["line"] . " " ; } + + /** + * Method to check if needed table is created + * + * @param array $tables Table name or an Array of table names to check + * + * @returns boolean True if table exists + */ + public function tableExists ($tables) { + $tables = !is_array ($tables) ? Array ($tables) : $tables; + $count = count ($tables); + if ($count == 0) + return false; + + array_walk ($tables, function (&$value, $key) { $value = self::$prefix . $value; }); + $this->where ('table_schema', $this->db); + $this->where ('table_name', $tables, 'IN'); + $this->get ('information_schema.tables', $count); + return $this->count == $count; + } } // END class ?> diff --git a/readme.md b/readme.md index aea818ea..d16a5b4c 100644 --- a/readme.md +++ b/readme.md @@ -525,19 +525,24 @@ if($db->has("users")) { } ``` ### Helper commands -Reconnect in case mysql connection died +Reconnect in case mysql connection died: ```php if (!$db->ping()) $db->connect() ``` -Get last executed SQL query. +Get last executed SQL query: Please note that function returns SQL query only for debugging purposes as its execution most likely will fail due missing quotes around char variables. ```php $db->get('users'); echo "Last executed query was ". $db->getLastQuery(); ``` +Check if table exists: +```php + if ($db->tableExists ('users')) + echo "hooray"; +``` ### Transaction helpers Please keep in mind that transactions are working on innoDB tables. Rollback transaction if insert fails: