PHP - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Create Table

PHP Create Table

shape Description

PHP Create Table requires following:

MySQL Create Table

shape Table

Initially, create a table with name "users2", with five columns: "id","Username", "Password","First", "Last". [sql] CREATE TABLE users2 ( ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, Username varchar(20), Password varchar(20), First varchar(20), Last varchar(20) )[/sql]

shape Examples

(MySQLi Object-oriented) [php] <!DOCTYPE html> <html> <body> <?php $HostName = "localhost"; $UserName = "root"; $Password = "password"; $dbname="accounts"; // Create connection $connection = new mysqli($HostName, $UserName, $Password, $dbname); // Check connection if ($connection->connect_error) { die("Connection failed: " . $connection->connect_error); } // sql to create table $sql = "CREATE TABLE users2 ( ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, Username varchar(20), Password varchar(20), First varchar(20), Last varchar(20) )"; if ($connection->query($sql) === TRUE) { echo "Table users2 created successfully"; } else { echo "Error creating table: " . $connection->error; } $connection->close(); ?> </body> </html> [/php] The output in the phpmyadmin looks like below. Example : (MySQLi Procedural) [php] <!DOCTYPE html> <html> <body> <?php $HostName = "localhost"; $UserName = "root"; $Password = "password"; $dbname="accounts"; // Create connection $connection = mysqli_connect($HostName, $UserName, $Password, $dbname); // Check connection if (!$connection) { die("Connection failed: " . mysqli_connect_error()); } // sql to create table $sql = "CREATE TABLE users2 ( ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, Username varchar(20), Password varchar(20), First varchar(20), Last varchar(20) )"; if (mysqli_query($connection, $sql)) { echo "Table Student created successfully"; } else { echo "Error creating table: " . mysqli_error($connection); } mysqli_close($connection); ?> </body> </html> [/php]

Summary

shape Description

  • SQL commands are used to create table.
  • Table can be created with the command CREATE TABLE