While extending a mysqli class, I ran into an issue with binding parameters. I've read that db names and table names should not be used as parameters, but while I was pondering this statement...
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$dbname'
I thought, as the db name is a string in this instance, a prepared query should work. Shouldn't it?
This works...
$stmt = $mysqli->query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$dbname'");
print_r( $stmt ); //shows [num_rows] = 1 if dbname exists
So if anybody could enlighten me as to why this doesn't work, I'd appreciate it...
$stmt = $mysqli->prepare("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = ?");
$stmt->$mysqli->bind_param("s", $dbname);
$stmt->$mysqli->execute();
print_r( $stmt ); //shows [num_rows] = 0 even if dbname exists
I should point our that the above code has been modified for clarity and that these exist inside methods with $this
instead of $mysqli
, but I don't think that has a bearing. This is really cooking my noodle :)
Method example:
public function dbExists($dbname)
{
$stmt = $this->query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$dbname'");
return $stmt->num_rows;
}
(but I don't like it due to unescaped input).