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 Install Composer

What is composer and how to install composer?

September 18, 2021September 21, 2021

Composer is a dependency manager tool for PHP. Using the composer we can manage the package dependency and version of the packages. Composer is different from npm (Node package manager) and apt-get because it only contain the dependency of project. Composer creates a compose.json file to contain the information about…

Read More
Php How to Move Files or Folder to One Folder to Another in Laravel

How to Move Files or Folder to One Folder to Another in Laravel ?

May 27, 2022

Move command is used to shift the folder or files from one location to another without copy so In this article i will show you move files or folder to one folder to another in Laravel. File System is used to perform the file system based operations like move files…

Read More

Create Custom Class or Custom Library in Laravel 10

March 14, 2023March 16, 2024

In Laravel, you can create custom classes or custom library in laravel that can be used throughout your application. These classes can be used to encapsulate common functionality, business logic, or to abstract away complex operations. Thus I this article i will show you to create a class and use…

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