Before creating a table what do you need to know?
A table is a self contained entity that consist of row(s) and column(s) what then is a row and what is a column
A row is a "horizontal" collection of item belonging to a certain category while
A column is a "vertical" collection.
If you are familiar with database you should know that each data has a specific data type which describe the nature of the data
below is a list of some data type:
1. char: use for text of a short width
2. var char: use for text of a longer range
3. int: use for numbers
4. date: use for time and date. etc.
to accurately manipulate your database here are some terms you need to know about
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\
now to our code
connect your database:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
note: PASSWORD AND USERNAME IS NOT THE SAME FOR ALL DATABASE.
NOW TO ADD OUR TABLE:
To add a table in MySQL we use the word INSERT
// sql to create table
$sql = "CREATE TABLE Student_info (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if ($conn->query($sql) === TRUE) {
echo "Table Student_info created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
$conn->close();
?>
there you have it
0 comments:
Post a Comment