Database Connectivity Methods in PHP Explained

undefined
 
Database Connectivity
 
 
M
y
S
Q
L
i
 
O
b
j
e
c
t
-
O
r
i
e
n
t
e
d
 
 
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username,
$password);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn-
>connect_error);
}
echo "Connected successfully";
?>
 
 
OUTPUT:
 
Connected successfully
 
M
Y
S
Q
L
I
 
P
R
O
C
E
D
U
R
A
L
 
 
 <?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn)
 
{
    die("Connection failed: " . mysqli_connect_error());
}
      echo "Connected successfully";
?>
 
 
OUTPUT:
 
Connected successfully
 
P
D
O
 
 
PDO - PHP Data Objects - is a database access layer
providing a uniform method of access to multiple
databases. It doesn't account for database-specific
syntax, but can allow for the process of switching
databases and platforms to be fairly painless, simply
by switching the connection string in many instances.
 
<?php
$servername = "localhost";
$username = "username";
$password = "password";
 
try {
    $conn
= new PDO("mysql:host=$servername;dbname=myDB",
$username, $password);
 
    // set 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
 
 
 
 
 
Slide Note
Embed
Share

This article explores different methods for establishing database connectivity in PHP, including MySQLi (Object-Oriented and Procedural), PDO (PHP Data Objects) explaining their implementation, error handling, and advantages. Learn how to connect to databases seamlessly and efficiently in PHP.

  • PHP
  • Database Connectivity
  • MySQLi
  • PDO
  • Error Handling

Uploaded on Sep 12, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Database Connectivity

  2. MySQLi MySQLiObject Object- -Oriented Oriented <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn- >connect_error); } echo "Connected successfully"; ?> OUTPUT: Connected successfully

  3. MYSQLI MYSQLIPROCEDURAL PROCEDURAL <?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?> OUTPUT: Connected successfully

  4. PDO PDO // set 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(); } ?> PDO - PHP Data Objects - is a database access layer providing a uniform method of access to multiple databases. It doesn't account for database-specific syntax, but can allow for the process of switching databases and platforms to be fairly painless, simply by switching the connection string in many instances. <?php $servername = "localhost"; $username = "username"; $password = "password"; try { OUTPUT: $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); Connected successfully

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#