I have this method
public function create_table($sql)
{
if (!$stmt = $this->prepare($sql))
exit('Error: '.$this->error);
return $stmt->execute();
}
It works with
$sql = "CREATE TABLE IF NOT EXISTS users (
id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,
username TEXT,
password TEXT,
usertype TEXT,
email TEXT,
joined INTEGER )";
if (!$db->create_table($sql)) {
echo 'error create table users';
}
But when I try to escape the $sql
with this
public function create_table($sql)
{
$sql = $this->escape_string($sql);
if (!$stmt = $this->prepare($sql))
exit('Error: '.$this->error);
return $stmt->execute();
}
I get the following error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\nid INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,\nusername TEXT,\npassword TEXT' at line 1
It is because the mysqli->escape_string()
escapes the newlines.
Is there a workaround that lets me use the escape of $sql?