I need to create multiple tables at once. Im having a hard time figuring out the correct method for accomplishing this. Currently my script looks like this:
private function buildDB() {
$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS headings (
type VARCHAR(150),
heading VARCHAR(100),
uniqueid VARCHAR(100)
)
CREATE TABLE IF NOT EXISTS titles (
type VARCHAR(150),
heading VARCHAR(100),
uniqueid VARCHAR(100)
)
MySQL_QUERY;
return mysql_query($sql);
}
Obviously, this doesn’t work and no tables are created. Is there a simple way for creating multiple tables at once?
MySQL is getting confused because you’re not delimiting your queries. Add a semicolon after the first
CREATEstatement:Also, make sure
MySQL_QUERYis at the beginning of the line with no other characters, except maybe a semicolon, as per the Heredoc documentation.Seeing as the above doesn’t appear to work, give this code a try:
You could use
mysqli_multi_query()(the MySQL version doesn’t exist), but you’d have to use MySQLi then. The above code returns the logical AND of the two queries, so you still get a0returned if one fails.