Create a database if it does not exist!
SQL

-Stands for Structured Query Language
Used to manage database //Update data on a database or retrieve data from a database
$servername="localhost"Connect to MySQL
$username="root"
$password=""
$link = mysql_connect($servername, $username, $password);Suppose we're creating a database with name 'test'. You must first of all make that db your current db.
if (!link)
{
die('Could not connect ' . mysql_error());
}
$db_selected = mysql_select_db('test', $link);You can now create your database with an appropriate name!
if (!db_selected)
{
$sql = 'CREATE DATABASE test';
if (mysql_query($sql, $link))
{
echo "Database test has been successfully made!";
require table.php;
}
else
{
echo "Error creating database: " . mysql_error() . "\n";
}
}
mysql_close($link);
?>
Note: You might be wondering why am I calling a table.php when the db is successfully created.
Well, this is not mandatory. I called the table.php because it will create a database table with given name.
Creating the database table
Suppose we are creating a database table with name test-table in our database.This time while connecting to MySQL, you should insert your $dbname!
$conn = new mysqli($servername, $username, $password, $dbname);
After connecting to MySQL, you can start creating your database table.
$sql = "CREATE TABLE test.test-table"We've created our table name with:
(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
)";
1. id //declared as Integer and it will auto increment as well with length 6
2. firstname //declared as Varchar and length 30
3. lastname //decared asVarchar with length 30
4. email // declared as Varchar with length 50
Check if table has been created successfully!
if ($conn->query($sql) === TRUE)Get in on github
{
echo "Table test_table created successfully";
}
else
{
echo "Error creating table: " . $conn->error;
}
?>