update core
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Plugi\Core;
|
||||
|
||||
use Exception;
|
||||
use PDO;
|
||||
|
||||
/**
|
||||
@@ -106,4 +107,38 @@ class DB
|
||||
$stmt = $this->pdo->prepare($query);
|
||||
return $stmt->execute($parameters);
|
||||
}
|
||||
|
||||
function tableExists($table) {
|
||||
|
||||
// Try a select statement against the table
|
||||
// Run it in try-catch in case PDO is in ERRMODE_EXCEPTION.
|
||||
try {
|
||||
$result = $this->pdo->query("SELECT 1 FROM {$table} LIMIT 1");
|
||||
} catch (Exception $e) {
|
||||
// We got an exception (table not found)
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
// Result is either boolean FALSE (no table found) or PDOStatement Object (table found)
|
||||
return $result !== FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a custom query with user input data passed as $parameters.
|
||||
*
|
||||
* @param string $table
|
||||
* @param string $sql
|
||||
* @return bool
|
||||
*/
|
||||
public function prepare(string $table, string $sql)
|
||||
{
|
||||
// check if table with the name $table exists. if not, execute $query.'
|
||||
try {
|
||||
$result = $this->pdo->query("SELECT 1 FROM {$table} LIMIT 1");
|
||||
return TRUE;
|
||||
} catch (Exception $e) {
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
return $stmt->execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user