PHP MySQL Create Table
From w3cyberlearnings
Contents |
PHP MySQL Create Table
Create a table is similar to create a database.
Syntax
$create_table = 'CREATE TABLE IF NOT EXISTS student ( student_id INT NOT NULL AUTO_INCREMENT, first_name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL, PRIMARY KEY(student_id) ) ENGINE=InnoDB'; mysql_query($create_table, $connection);
Example 1
<?php define('HOST', 'localhost'); define('USER', 'root'); define('PASS', 'yeething'); define('DBNAME', 'woowood'); $connection = mysql_connect(HOST, USER, PASS); if (!$connection) { die("can not connect to the server!<br/>"); } else { $rdb = mysql_select_db(DBNAME); if (!$rdb) { die("The " . DBNAME . "database could not be selected"); } else { // student table $create_table = 'CREATE TABLE IF NOT EXISTS student ( student_id INT NOT NULL AUTO_INCREMENT, first_name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL, PRIMARY KEY(student_id) ) ENGINE=InnoDB'; if (!mysql_query($create_table, $connection)) { echo "Can't create table: " . mysql_error($connection) . "<br/>"; } else { echo "You have successfully create student table <br/>"; } } } mysql_close($connection); ?>
Output
You have successfully create student table