Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Lecture 19 - PHP Connect To MySQL

This document discusses connecting to a MySQL database from PHP. It covers using the MySQLi extension in both object-oriented and procedural styles. The MySQLi extension allows connecting to and interacting with a MySQL database from PHP. The document provides code examples for opening a connection, checking for connection errors, and closing the connection. It also discusses when to use MySQLi versus PDO for connecting to MySQL from PHP.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture 19 - PHP Connect To MySQL

This document discusses connecting to a MySQL database from PHP. It covers using the MySQLi extension in both object-oriented and procedural styles. The MySQLi extension allows connecting to and interacting with a MySQL database from PHP. The document provides code examples for opening a connection, checking for connection errors, and closing the connection. It also discusses when to use MySQLi versus PDO for connecting to MySQL from PHP.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

PHP 

Connect to MySQL

Dr. Fareed Ahmed Jokhio


PHP Connect to MySQL
• PHP Connect to MySQL
• MySQLi extension (the "i" stands for
improved)
• PDO (PHP Data Objects)
• Earlier versions of PHP used the MySQL
extension. However, this extension was
deprecated in 2012.
Should I Use MySQLi or PDO?
• If you need a short answer, it would be
"Whatever you like".
• Both MySQLi and PDO have their advantages:
• PDO will work on 12 different database
systems, whereas MySQLi will only work with
MySQL databases.
Should I Use MySQLi or PDO?
• So, if you have to switch your project to use another
database, PDO makes the process easy. You only have
to change the connection string and a few queries.
With MySQLi, you will need to rewrite the entire code
- queries included.
• Both are object-oriented, but MySQLi also offers a
procedural API.
• Both support Prepared Statements. Prepared
Statements protect from SQL injection, and are very
important for web application security.
MySQL Examples in MySQLi Syntax
• In this, and in the following lectures we
demonstrate two ways of working with PHP
and MySQL:
• MySQLi (object-oriented)
• MySQLi (procedural)
MySQLi Installation
• For Linux and Windows: The MySQLi extension
is automatically installed in most cases, when
php5 mysql package is installed.
• For installation details, go to: 
http://php.net/manual/en/mysqli.installation.
php
Open a Connection to MySQL
• Before we can access data in the MySQL
database, we need to be able to connect to
the server:
Example (MySQLi Object-Oriented)
• <?php
• $servername = "localhost";
• $username = "root";
• $password = "";

• // Create connection
• $conn = new mysqli($servername, $username, $password);

• // Check connection
• if ($conn->connect_error) {
• die("Connection failed: " . $conn->connect_error);
• }
• echo "Connected successfully";
• ?>
Example (MySQLi Procedural)
• <?php
• $servername = "localhost";
• $username = "root";
• $password = "";

• // Create connection
• $conn = mysqli_connect($servername, $username, $password);

• // Check connection
• if (!$conn) {
• die("Connection failed: " . mysqli_connect_error());
• }
• echo "Connected successfully";
• ?>
Close the Connection
• The connection will be closed automatically
when the script ends. To close the connection
before, use the following:
• MySQLi Object-Oriented:
• $conn->close();
• MySQLi Procedural:
• mysqli_close($conn);

You might also like