Skip to content
Merged

fixes #293

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion MysqliDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
?>
9 changes: 7 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down