Skip to content

Commit 9bbea7a

Browse files
committed
Added tableExists
1 parent bc6ddff commit 9bbea7a

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

MysqliDb.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,5 +1457,25 @@ private function _traceGetCaller () {
14571457
return __CLASS__ . "->" . $caller["function"] . "() >> file \"" .
14581458
str_replace ($this->traceStripPrefix, '', $caller["file"] ) . "\" line #" . $caller["line"] . " " ;
14591459
}
1460+
1461+
/**
1462+
* Method to check if needed table is created
1463+
*
1464+
* @param array $tables Table name or an Array of table names to check
1465+
*
1466+
* @returns boolean True if table exists
1467+
*/
1468+
public function tableExists ($tables) {
1469+
$tables = !is_array ($tables) ? Array ($tables) : $tables;
1470+
$count = count ($tables);
1471+
if ($count == 0)
1472+
return false;
1473+
1474+
array_walk ($tables, function (&$value, $key) { $value = self::$prefix . $value; });
1475+
$this->where ('table_schema', $this->db);
1476+
$this->where ('table_name', $tables, 'IN');
1477+
$this->get ('information_schema.tables', $count);
1478+
return $this->count == $count;
1479+
}
14601480
} // END class
14611481
?>

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)