Database Connectivity Methods in PHP Explained

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.


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. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

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

Related


More Related Content