Web Hosting Talk







View Full Version : Create Multiple Tables MYSQL


raulgonzalez
09-10-2007, 07:10 PM
I can't seem to be able to create multiple tables at the same time. What's wrong with the code below?

It only creates the last table, what should I change? Please Help


<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL & ~E_NOTICE);
// Connect and select.
if ($dbc = @mysql_connect ('localhost', 'root', 'pass')) {
if (!@mysql_select_db ('database')) {
die ('<p>Could select the database because: <b>' . mysql_error() . '</b></p>');
}
} else {
die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');
}


$query = 'create table table1
(bla bla bla
bla bla bla)';

$query = 'create table table2
(bla bla bla
bla bla bla)';

// Run the query.
if (@mysql_query ($query)) {
print '<p>The table has been created.</p>';
} else {
die ('<p>Could not create the table because: <b>' . mysql_error() . '</b>.</p><p>The query being run was: ' . $query . '</p>');
}

mysql_close(); // Close the connection.
?>

raulgonzalez
09-10-2007, 07:49 PM
It seems like I need a break. I found that the code below did the trick. Duh!

<?php
// Connects to your Database
mysql_connect("localhost", "root", "pass") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

mysql_query("CREATE TABLE one ( name VARCHAR(30),
age INT, car VARCHAR(30))");

mysql_query("CREATE TABLE two ( name VARCHAR(30),
age INT, car VARCHAR(30))");

mysql_query("CREATE TABLE tree ( name VARCHAR(30),
age INT, car VARCHAR(30))");

Print "Your table(s) has/have been created";
?>

Thank you.