diff --git a/MysqliDb.php b/MysqliDb.php index 75eb0768..4c722a82 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); @@ -1453,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: