Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Lecture 21 - PHP MySQL Create Table

The document discusses how to create a MySQL table using MySQLi in PHP. It explains that a table has a unique name and consists of columns and rows. It then shows the SQL CREATE TABLE statement used to create a table named "MyGuests" with five columns - id, firstname, lastname, email, and reg_date. It also describes various column attributes like PRIMARY KEY, NOT NULL, AUTO_INCREMENT. Finally, it provides an example PHP code to connect to the database and execute the CREATE TABLE statement.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lecture 21 - PHP MySQL Create Table

The document discusses how to create a MySQL table using MySQLi in PHP. It explains that a table has a unique name and consists of columns and rows. It then shows the SQL CREATE TABLE statement used to create a table named "MyGuests" with five columns - id, firstname, lastname, email, and reg_date. It also describes various column attributes like PRIMARY KEY, NOT NULL, AUTO_INCREMENT. Finally, it provides an example PHP code to connect to the database and execute the CREATE TABLE statement.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PHP 

MySQL Create Table

Dr. Fareed Ahmed Jokhio


MySQL Create Table
• A database table has its own unique name and
consists of columns and rows.
Create a MySQL Table Using MySQLi

• The CREATE TABLE statement is used to create


a table in MySQL.
• We will create a table named "MyGuests",
with five columns: "id", "firstname",
"lastname", "email" and "reg_date":
Create a MySQL Table Using MySQLi
CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY
KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT
CURRENT_TIMESTAMP ON UPDATE
CURRENT_TIMESTAMP
)
Create a MySQL Table Using MySQLi

• Notes on the table above:


Create a MySQL Table Using MySQLi

• The data type specifies what type of data the


column can hold.
• After the data type, you can specify other
optional attributes for each column:
Create a MySQL Table Using MySQLi
• NOT NULL - Each row must contain a value for that column,
null values are not allowed
• DEFAULT value - Set a default value that is added when no
other value is passed
• UNSIGNED - Used for number types, limits the stored data to
positive numbers and zero
• AUTO INCREMENT - MySQL automatically increases the
value of the field by 1 each time a new record is added
• PRIMARY KEY - Used to uniquely identify the rows in a table.
The column with PRIMARY KEY setting is often an ID number,
and is often used with AUTO_INCREMENT
Create a MySQL Table Using MySQLi

• Each table should have a primary key column


(in this case: the "id" column). Its value must
be unique for each record in the table.
• The following examples shows how to create
the table in PHP:
Example (MySQLi Object-oriented)
• <?php
• $servername = "localhost";
• $username = “root";
• $password = "";
• $dbname = "myDB";

• // Create connection
• $conn = new mysqli($servername, $username, $password, $dbname);
• // Check connection
• if ($conn->connect_error) {
• die("Connection failed: " . $conn->connect_error);
• }
Example (MySQLi Object-oriented)
• // sql to create table
• $sql = "CREATE TABLE MyGuests (
• id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
• firstname VARCHAR(30) NOT NULL,
• lastname VARCHAR(30) NOT NULL,
• email VARCHAR(50),
• reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP
• )";
Example (MySQLi Object-oriented)
• if ($conn->query($sql) === TRUE) {
• echo "Table MyGuests created successfully";
• } else {
• echo "Error creating table: " . $conn->error;
• }

• $conn->close();
• ?>
Example (MySQLi Object-oriented)
• <?php
• $servername = "localhost";
• $username = "root";
• $password = "";
• $dbname = "myDB";

• // Create connection
• $conn = mysqli_connect($servername, $username, $password, $dbname);
• // Check connection
• if (!$conn) {
• die("Connection failed: " . mysqli_connect_error());
• }
Example (MySQLi Object-oriented)
• // sql to create table
• $sql = "CREATE TABLE MyGuests (
• id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
• firstname VARCHAR(30) NOT NULL,
• lastname VARCHAR(30) NOT NULL,
• email VARCHAR(50),
• reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP
• )";
Example (MySQLi Object-oriented)
• if (mysqli_query($conn, $sql)) {
• echo "Table MyGuests created successfully";
• } else {
• echo "Error creating table: " . mysqli_error($conn);
• }

• mysqli_close($conn);
• ?>

You might also like