ZenDB conversion question
3 posts by 2 authors in: Forums > CMS Builder
Last Post: Thursday at 12:33pm (RSS)
I have an existing function to test if a table exists.:
function testIfTableExists($table)
{
global $TABLE_PREFIX;
$sql = "SHOW TABLES LIKE '$TABLE_PREFIX$table'";
$val = mysqli()->query($sql);
return mysqli()->affected_rows > 0 ? true : false;
}
I am trying to update it to DB::query($sql), but the result is an empty array. Running the query in the MySQL console shows a single result with the table name.
Using ZenDB, what would be the recommended way to test if a table exists?
Instead of reinventing the wheel, is there a CMSB function determining whether a table exists?
By Dave - Thursday at 12:14pm
Hi Jeff,
Glad to see you're trying out those new functions. We have a built-in ZenDB function for that now:
DB::tableExists($table)
Which does this internally:
public static function tableExists(string $tableName): bool
{
$fullTable = self::getFullTable($tableName);
return self::query("SHOW TABLES LIKE ?", $fullTable)->count() > 0;
}
You can pass it a table with or without a table prefix, and it will add one if needed.
Hope that helps! Let me know any other questions.
interactivetools.com