Skip to content

Commit 00824c1

Browse files
committed
Merge pull request #293 from avbdr/master
fixes
2 parents 031d510 + 9bbea7a commit 00824c1

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

MysqliDb.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,11 @@ public function get($tableName, $numRows = null, $columns = '*')
443443
$columns = '*';
444444

445445
$column = is_array($columns) ? implode(', ', $columns) : $columns;
446-
$this->_tableName = self::$prefix . $tableName;
446+
if (strpos ($tableName, '.') === false)
447+
$this->_tableName = self::$prefix . $tableName;
448+
else
449+
$this->_tableName = $tableName;
450+
447451
$this->_query = 'SELECT ' . implode(' ', $this->_queryOptions) . ' ' .
448452
$column . " FROM " . $this->_tableName;
449453
$stmt = $this->_buildQuery($numRows);
@@ -1459,5 +1463,25 @@ private function _traceGetCaller () {
14591463
return __CLASS__ . "->" . $caller["function"] . "() >> file \"" .
14601464
str_replace ($this->traceStripPrefix, '', $caller["file"] ) . "\" line #" . $caller["line"] . " " ;
14611465
}
1466+
1467+
/**
1468+
* Method to check if needed table is created
1469+
*
1470+
* @param array $tables Table name or an Array of table names to check
1471+
*
1472+
* @returns boolean True if table exists
1473+
*/
1474+
public function tableExists ($tables) {
1475+
$tables = !is_array ($tables) ? Array ($tables) : $tables;
1476+
$count = count ($tables);
1477+
if ($count == 0)
1478+
return false;
1479+
1480+
array_walk ($tables, function (&$value, $key) { $value = self::$prefix . $value; });
1481+
$this->where ('table_schema', $this->db);
1482+
$this->where ('table_name', $tables, 'IN');
1483+
$this->get ('information_schema.tables', $count);
1484+
return $this->count == $count;
1485+
}
14621486
} // END class
14631487
?>

readme.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,19 +525,24 @@ if($db->has("users")) {
525525
}
526526
```
527527
### Helper commands
528-
Reconnect in case mysql connection died
528+
Reconnect in case mysql connection died:
529529
```php
530530
if (!$db->ping())
531531
$db->connect()
532532
```
533533

534-
Get last executed SQL query.
534+
Get last executed SQL query:
535535
Please note that function returns SQL query only for debugging purposes as its execution most likely will fail due missing quotes around char variables.
536536
```php
537537
$db->get('users');
538538
echo "Last executed query was ". $db->getLastQuery();
539539
```
540540

541+
Check if table exists:
542+
```php
543+
if ($db->tableExists ('users'))
544+
echo "hooray";
545+
```
541546
### Transaction helpers
542547
Please keep in mind that transactions are working on innoDB tables.
543548
Rollback transaction if insert fails:

0 commit comments

Comments
 (0)