Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm not sure if this is a bug or the intended behaviour :
db_table_exists(NULL) or db_table_exists(empty string) return the number of tables in the database.
Yet you'd expect code like the following would prevent you from errors :
if (db_table_exists($var)) {
db_query('SELECT FROM {'. $var .'} WHERE ...');
}
Comment | File | Size | Author |
---|---|---|---|
#5 | db_table_exists_1.patch | 2.2 KB | kbahey |
#2 | db_table_exists_0.patch | 1.26 KB | yched |
Comments
Comment #1
yched CreditAttribution: yched commentedThat's with MySQL 4.1, btw
Comment #2
yched CreditAttribution: yched commentedActually I think it's a bug.
It does not match the function description, and I don't think the pgsql equivalent behaves the same (it uses a "WHERE name = $tablename" style construct.
Attached patch checks if the provided name is empty and returns 0. (usual PHP 'empty' stuff : it will also reject '0', which I think is not a valid table name anyway - could be fixed, but I did not want to add too much cruft)
It do not think this is baby sitting broken code either :
cannot be considered broken code IMO, and should be considered 'the right way'
Comment #3
bdragon CreditAttribution: bdragon commentedHow about this?
Comment #4
drummIf you want to return FALSE, then return that and not the number 0.
Comment #5
kbahey CreditAttribution: kbahey commentedHere is a patch to mysql, mysqli, and pgsql.
It checks that $table is indeed passed in, and if so, it executes the query, otherwise it returns FALSE;
Comment #6
keith.smith CreditAttribution: keith.smith commentedpatch no longer applies
# patch -p0 < db_table_exists_1.patch
patching file includes/database.mysqli.inc
Hunk #1 FAILED at 398.
1 out of 1 hunk FAILED -- saving rejects to file includes/database.mysqli.inc.rej
patching file includes/database.mysql.inc
Hunk #1 FAILED at 418.
1 out of 1 hunk FAILED -- saving rejects to file includes/database.mysql.inc.rej
patching file includes/database.pgsql.inc
Hunk #1 FAILED at 391.
1 out of 1 hunk FAILED -- saving rejects to file includes/database.pgsql.inc.rej
Comment #7
dpearcefl CreditAttribution: dpearcefl commentedIs there any interest or a need for this this be fixed?