Hi,
I was wondering why the CREATE TABLE query in my code seems to be called automatically, whilst in the example below from w3schools, there is a line which explicitly calls the CREATE TABLE query.
Why is that the query in the w3schools example isn't ran twice?
My code:
<?php
$connectionname = mysql_connect("localhost","XXX","XXX");
if (!$connectionname)
{
die('Could not connect: ' . mysql_error());
}
// Create table
mysql_select_db("databasename", $connectionname);
$sqlcommandvariable =
"
CREATE TABLE Userssss
(
userID int(8),
userName varchar(15),
firstName varchar(15),
lastName varchar(15),
password varchar(32)
)
";
if (!mysql_query("$sqlcommandvariable")) die(mysql_error());
else {
echo "success in table creation.";
}
mysql_close($connectionname);
?>
W3schools example:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
// Create table
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE Persons
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
// Execute query. Why is this called explicitly? When my query runs automatically?
mysql_query($sql,$con);
mysql_close($con);
?>
I'm finding this confusing, so if anyone could explain where the difference is I'd most appreciate it.
Many thanks! :)