MySQL Database In the PHP for Storing Data
Creating a Connection to My SQL Database by using the PHP Language
MySQL is a database management system, not a programming language.MySQL uses structured
query language (SQL) to manage data inside a database.The data in MySQL databases are stored
in tables.A table is a collection of related sata, and it consists of columns and rows.Databases are
useful for storing information categorically.
The “mysqli” function is used to connect to one database using php language.This provides some
connection between front end and back end language.The following is the syntax of creating one
connection to a database in php.
$connection = mysqli_connect($servername, $username, $password, $DBname);
Example : The following example creates a connection to a database named as the "studentsDB”
<?php
$servername = "localhost" ;
$username = "username" ;
$password = "password" ;
$DBname = “studentsDB” ;
// Create connection
$connection = mysqli_connect($servername, $username, $password);
// Check connection
if ($connection->connect_error) {
die( "Connection failed: " . mysqli_connect_error( ) );
// Connection not successful
echo "Connected successfully";
?>
Closing a Connection to My SQL Database by using the PHP Language
In order to close connection in the mysql database we use the php function mysqli_close( ) which
disconnects from the database.It requires one parameter which is one connection returned by the
mysql_connect function.The following is the syntax of creating one connection to some database
in php.
mysqli_close(connection ) ;
If the parameter is not specified in the mysqli_close( ) function then the one last opened database
is closed.This function returns true if it closes the connection successfully otherwise it returnw as
false if it does not close the connection successfully.
Example :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Creating connection
$connection = mysqli_connect($servername, $username, $password);
// Checking connection
if ($connection->connect_error) {
die("Connection failed: " . mysqli_connect_error( ));
// Creating a database named newDB
$sql = "CREATE DATABASE newDB";
if (mysqli_query($connection, $sql)) {
echo "Database created successfully with the name newDB";
} else {
echo "Error creating database: " . mysqli_error($connection);
// closing connection
mysqli_close($connection);
?>
Creating the MySQL Database by using the PHP Language
A database consists of one or more data tables.You will need some special CREATE privileges to
create or to delete a MySQL database.The create database statement is a statement which is used
to create a database in MySQL.The following example create a database named "myDB":
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$connection = new mysqli($servername, $username, $password);
// Check connection
if ($connection->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Create database
$sql = " Create Database myDB ";
if ($connection->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $connection->error;
$conn->close();
?>
Select the Data From MySQL Database by using the PHP Language
The following example selects the id, firstname and lastname columns from the MyGuests table
and displays it on the web page as,
<?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);
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
$conn->close();
?>
Insert the Data into MySQL Database by using the PHP Language
After a database and a table have been created, we can start adding data in them.Here are some
syntax rules to follow as The SQL query must be quoted in the PHP String values inside the SQL
query must be quoted.Numeric values must not be quoted and word NULL must not be quoted in
PHP.The INSERT INTO statement is used to add new records to a MySQL table such as,
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...);
In the previous chapter we created an empty table named "MyGuests" with five columns: "id",
"firstname", "lastname", "email" and "reg_date". Now, let us fill the table with data.
Note this If some column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP with
default update of current_timesamp (like the "reg_date" column) there is no need to be specified
in the SQL query MySQL will automatically add the value.The following example adds one new
record to the "MyGuests" table,
<?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);
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
$conn->close();