Database Operations with Php





Database Operations with Php

Database operations with Php are quite easier and straight-forward . People sometimes find it difficult to connect or operate the databases with Php programming language because either they are using the old means to connect/ operate the databases or they implement the newest connectors with the old versions of Php.

We will discuss the mysqli_connect to connect and manipulate the database and it is the best practice to remain on top of the curve as far as new versions of the programming environment are concerned.

<?php

$cn=mysqli_connect(“localhost”,”database_username”,”database_password”,”database_name”);

if (mysqli_connect_errno($cn))
{
echo ” Connection failed due to the following error :” . mysqli_connect_error();
}
?>

The above example simply connects to the database installed at our computer in which we have our Php application. The connection object “cn” is initialize with mysqli_connect() construct with the four parameters.
We then check for the successfulness of the connection. If it fails, we will get the error code from last connect call
with the mysqli_connect_errno() construct carrying connection object as the parameter.Quite simple 🙂

Our previous example showed how to connect to the existing database. We will go through the CRUD  operations  and show you the examples about how to perform them accurately.

1. Creating the database named Test:

The creation of the database is our first step. This can be achieved by connecting with the server. After that, creating the database.

 

<?php
$cn=mysqli_connect(“localhost”,”my_username”,”my_password”); //connecting the serverif (mysqli_connect_errno())
{
echo “Error in establisihng the connection: ” . mysqli_connect_error();
}$query=”CREATE DATABASE  TEST”;
if (mysqli_query($cn,$query))
{
echo “Database has been created”;
}
else
{
echo “Error while creating the database: ” . mysqli_error($cn);
}
?>

 

1. Creating the table within our database:

The process of creating the table is as easy as creating the database. We have to execute the create table query using the mysqli construct

 

<?php
$cn=mysqli_connect(“localhost”,”my_username”,”my_password”,”Test”);if (mysqli_connect_errno())
{
echo “Connection failed : ” . mysqli_connect_error();
}$query=”CREATE TABLE  Employees(firstName VARCHAR(18), lastName VARCHAR(18), salary DECIMAL(5,4) )”;if (mysqli_query($cn,$query))
{
echo “Employees Table created successfully”;
}
else
{
echo “Error encountered  while creating the table : ” . mysqli_error($cn);
}
?>

 

3. Inserting the records

<?php
$cn=mysqli_connect(“localhost”,”my_username”,”my_password”,”Test”);if (mysqli_connect_errno())
{
echo “Connection failed : ” . mysqli_connect_error();
}mysqli_query($cn,”INSERT INTO Employees (firstName, lastName, salary) VALUES (‘George’,’Smith’ ,55000)”);

mysqli_close($cn);

?>

4.Updating the records

<?php
$cn=mysqli_connect(“localhost”,”my_username”,”my_password”,”Test”);
if (mysqli_connect_errno())
{
echo “Connection failed : ” . mysqli_connect_error();
}
mysqli_query($cn,”Update Employees Set salary=6000 Where firstName=’George’ AND lastName=’Smith’ “);
mysqli_close($cn);
?>

5.Deleting the records

<?php
$cn=mysqli_connect(“localhost”,”my_username”,”my_password”,”Test”);
if (mysqli_connect_errno())
{
echo “Connection failed : ” . mysqli_connect_error();
}
mysqli_query($cn,”Delete From Employees Where firstName=’George’ AND lastName=’Smith’ “);
mysqli_close($cn);
?>


Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.