Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
connect php to mysql

How to connect PHP to MySQL ?

Aman Jain, September 27, 2021September 29, 2021

PHP is commonly used in the world of web. PHP has many built in tools to connect with different drivers and in this tutorial we are going to use MySQL driver to connection between PHP and MySQL.

There is two to connect database in PHP 5 and later version.

  1. MySQLi extension
  2. PDO (PHP Data Obect)

As the name implies that MySQLi i stands for improved version of mysql php driver and mysqli only supports one database but in PHP PDO, its supports 12 databases.

Both Mysqli and PDO supports object oriented buy mysqli also supports procedural functions.

Connect php to mysql using PDO

Connection mysql to updo requires PDO extension, which you can install from official PHP website.

Install PDO for php

After installation of updo create a new file for connection.

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

Connect php to mysql using MySQLi

Most of the php comes with mysqli extension and no other installation required to connect the php using mysqli.

<?php 

$servername = "localhost";
$username = "root";
$password = "";
$database = 'test'

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

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
 
?>

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

Related

Php Mysql

Post navigation

Previous post
Next post

Related Posts

Devops How to Install multiple versions of PHP?

How to Install multiple versions of PHP?

September 18, 2021September 21, 2021

Sometimes it’s required to install the different version of PHP rather then the default one of Ubuntu Repository System. To install the different version we will follow the basic steps before installation of PHP Step 1 : Add ppa:ondrej/php to Ubuntu system Open the terminal or ssh connection and then…

Read More
Php How to Get Current Url in Blade File Laravel

How to Get Current Url in Blade File Laravel ?

October 27, 2022March 16, 2024

Sometimes in our application we want to Get Current Url in Blade file Laravel because we want to add specific conditions to show the data on behalf of current url. It can be complete url, route name and query params too so that we can fetch the data accordingly and…

Read More
Php Laravel 8 generate qr code

How to generate qr code in Laravel 8?

October 16, 2021November 16, 2021

In this article we will learn to generate the qr code in Laravel. we will use a simple example to generate the qr code. As we know qr code is stand for Quick Response and we can scan and get the information from it. we can use qr code in…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version