Chapter 05
Chapter 05
Chapter 05
Marks: 14 Marks
1.Create database
<?php
$servername = "localhost";
$username = "root";
$password = "";/* Put your password here*/
/* Create connection*/
$conn = mysqli_connect($servername, $username, $password);
/* Check connection*/
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
/* Create database*/
$sql = "CREATE DATABASE stud";
if (mysqli_query($conn, $sql)) {
echo " $sql created successfully";
}else{
echo "Error creating database: " . mysqli_error($conn);
}
mysqli_close($conn);
The die():- is an inbuilt function in PHP. It is used to print message and exit from the current php
script. It is equivalent to the exit() function in PHP.
Syntax :
die($message)
Parameters : This function accepts only one parameter and which is not mandatory to be
passed.
$message : This parameter represents the message to be printed while exiting from script.
1.Using MySQLi object-oriented procedure: We can use the MySQLi object-oriented procedure
to establish a connection to MySQL database from a PHP script.
Syntax:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Creating connection
$conn = new mysqli($servername, $username, $password);
// Checking connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Output:
Connected successfully
Explanation: We can create an instance of the mysqli class providing all the necessary details
required to establish the connection such as host, username, password etc. If the instance is
created successfully then the connection is successful otherwise there is some error in
establishing connection.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Creating connection
$conn = mysqli_connect($servername, $username, $password);
// Checking connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
Explanation: In MySQLi procedural approach instead of creating an instance we can use the
mysqli_connect() function available in PHP to establish a connection. This function takes the
information as arguments such as host, username , password , database name etc. This function
returns MySQL link identifier on successful connection or FALSE when failed to establish a
connection.
3.Using PDO procedure: PDO stands for PHP Data Objects. That is, in this method we connect to
the database using data objects in PHP as described below:
Syntax:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// setting the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
Output:
Connected successfully
Explanation:The exception class in PDO is used to handle any problems that may occur in our
database queries. If an exception is thrown within the try{ } block, the script stops executing and
flows directly to the first catch(){ } block.
Closing A Connection
When we establish a connection to a MySQL database from a PHP script , we should also
disconnect or close the connection when our work is finished. Here we have described the
1.Insert operation
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stud";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
2. Retrieve Data
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stud";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]."-Email:"."
".$row["email"]."-Time"." ".$row["reg_date"]."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
3. Update data
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stud";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
?>
4.Delete Data.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "stud";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>