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

Php How to Fetch Records Between Two Date Range in Laravel

How to Fetch Records Between Two Date Range in Laravel ?

September 6, 2022March 16, 2024

Many times we want to fetch records between two date range in laravel like find all records which is created between start yesterday and end today so in that case we need to check it with created_at columns in database but created_at column also has time and if we directly…

Read More
Php make a component in laravel 10

How to create component in Laravel 10 ?

March 23, 2023March 16, 2024

In this tutorial i will show you simple html based component in Laravel 10 using blade. Creating reusable components is a fundamental aspect of modern web development, as it allows developers to simplify code and improve efficiency. Laravel 10, one of the most popular PHP frameworks, comes with a robust…

Read More

What is Access Modifiers ?

July 18, 2021August 2, 2021

In object oriented programming(oops) to set the visibility of classes, variables and methods we are using access modifiers. So Access modifiers are used the encapsulation of class properties. There are three types of access modifiers in any language (Php, java or c etc. ) Private Protected Public 1. Private Access…

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

  • June 2025
  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version