The Wayback Machine - https://web.archive.org/web/20240624020146/https://www.geeksforgeeks.org/connect-php-to-mysql/
Open In App

Connect PHP to MySQL

Last Updated : 03 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

What is MySQL?

MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP.

Structured Query Language (SQL). The data in a MySQL database are stored in tables that consist of columns and rows.

MySQL is a database system that runs on a server. MySQL is ideal for both small and large applications. MySQL is a very fast, reliable, and easy-to-use database system. It uses standard SQL. MySQL compiles on a number of platforms.

How we can connect PHP to MySQL?

PHP 5 and later can work with a MySQL database using:

  • MySQLi extension (the ‘i’ is abbreviation for improved)
  • PDO (PHP Data Objects)

 Which one should we use MySQLi or PDO?

Both MySQLi and PDO have their recompenses:

  • PDO will work with 12 different database systems, whereas MySQLi will only work with MySQL databases.
  • So, if you have to shift your project to use alternative 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 complete code — queries included.
  • Both are object-oriented, but MySQLi also offers a procedural API.

In short, you can choose whichever you want if you want to stick to MySQL otherwise you should go with PDO.

Connection to MySQL using MySQLi

PHP provides mysql_connect() function to open a database connection.

This function takes a single parameter, which is a connection returned by the mysql_connect() function.

You can disconnect from the MySQL database anytime using another PHP function mysql_close().

There is also a procedural approach of MySQLi to establish a connection to MySQL database from a PHP script.

It can be done in two ways:

MySQLi Object-Oriented

        

PHP




<?php
$servername = "localhost";
$username = "username";
$password = "password";
 
// Connection
$conn = new mysqli($servername,
           $username, $password);
 
// For checking if connection is
// successful or not
if ($conn->connect_error) {
  die("Connection failed: "
      . $conn->connect_error);
}
echo "Connected successfully";
?>


MySQLi Procedural

PHP




<?php
 
$servername = "localhost";
$username = "username";
$password = "password";
 
// Connection
$conn = mysqli_connect($servername,
               $username, $password);
 
// Check if connection is
// Successful or not
if (!$conn) {
  die("Connection failed: "
      . mysqli_connect_error());
}
echo "Connected successfully";
?>


Connection to MySQL using PDO

PHP




<?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();
}
?>




Previous Article
Next Article

Similar Reads

How to connect the components using Connect() in React Redux
In React Redux, you use the connect() function to connect your components to the Redux store. This function takes two parameters: mapStateToProps and mapDispatchToProps. mapStateToProps maps the store's state to the component's props, while mapDispatchToProps maps dispatch actions to props. By using connect( ), your components can access the store'
3 min read
How to connect multiple MySQL databases on a single webpage ?
This article explains how to connect multiple MySQL databases into a single webpage. It is useful to access data from multiple databases. There are two methods to connect multiple MySQL databases into a single webpage which are: Using MySQLi (Improved version of MySQL)Using PDO (PHP Data Objects) Syntax: MySQLi Procedural syntax: $link = mysqli_con
2 min read
How to connect Express application to MongoDB and MySQL?
Express JS is a JavaScript framework that is used for building the backend of web and mobile applications. It works on top of Node JS which is helpful for managing servers and routes. Connecting an Express JS application to MongoDB and MySQL requires installing Node.js in your computer system. In this article, we will see how we can connect the Exp
5 min read
Node.js Connect Mysql with Node app
Node.js is a powerful platform for building server-side applications, and MySQL is a widely used relational database. Connecting these two can enable developers to build robust, data-driven applications. In this article, we'll explore how to connect a Node.js application with a MySQL database, covering the necessary setup, configuration, and basic
2 min read
How to Connect Node.js Application to MySQL ?
To connect the Node App to the MySQL database we can utilize the mysql package from Node Package Manager. This module provides pre-defined methods to create connections, query execution and perform other database related operations. Approach to Connect Node App to MySQLFirst, initialize the node.js project in the particular folder on your machine.D
2 min read
How to connect the Database with PHP DOM page ?
In this article, we will see how to connect the database with the PHP DOM page? Approach: Make sure you have XAMPP or WAMP installed on your machine. In this tutorial, we will be using the XAMPP server. To connect the database with the PHP DOM page, follow the steps given below: 1. Create Database: First, we will create a database named 'geeksforge
3 min read
How to connect ODBC Database with PHP?
The Microsoft Open Database Connectivity (ODBC) is a C application programming language interface. So the applications can access data from different database management systems. The designers of ODBC aimed to make it independent of database systems and operating systems. In this article, we are using the Microsoft Access database to establish a co
2 min read
How to connect ReactJS as a front-end with PHP as a back-end ?
Connecting the front end of React with the PHP backend is a classic approach to building a web application with the front end for the user interface and the back end or server side for handling databases, programming logic, and other operations. React JSReact JS is a declarative, efficient, and flexible JavaScript library for building user interfac
3 min read
How to connect to an API in JavaScript ?
An API or Application Programming Interface is an intermediary which carries request/response data between the endpoints of a channel. We can visualize an analogy of API to that of a waiter in a restaurant. A typical waiter in a restaurant would welcome you and ask for your order. He/She confirms the same and carries this message and posts it to th
5 min read
How to Display Flash Messages using connect-flash Module in Node.js ?
Connect-flash module for Node.js allows the developers to send a message whenever a user is redirecting to a specified web-page. For example, whenever, a user successfully logged in to his/her account, a message is flashed(displayed) indicating his/her success in the authentication.Prerequisites Before starting with the application, you must have t
3 min read
three90RightbarBannerImg