Create a database if it does not exist!
-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
$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.
die('Could not connect ' . mysql_error());
$db_selected = mysql_select_db('test', $link);You can now create your database with an appropriate name!
$sql = 'CREATE DATABASE test';
if (mysql_query($sql, $link))
echo "Database test has been successfully made!";
echo "Error creating database: " . mysql_error() . "\n";
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 tableSuppose 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,
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";
echo "Error creating table: " . $conn->error;