Developer Corner

Do you have a question about web development? Is something just not working right? Get answers and solutions here.

 

How to connect to MySQL using PHP

This article describes several methods for connecting to a MySQL database using PHP:

  • MySQL Improved (mysqli) extension
  • PDO (PHP Data Objects)
  • Legacy MySQL (mysql_) functions
  • Connecting to a remote MySQL database using PHP
The MySQL databases and users must already exist before you can use these methods. For information about how to manage MySQL databases using cPanel.
Table of Contents


CONNECTING TO MYSQL USING THE MYSQL IMPROVED EXTENSION

The MySQL Improved extension uses the mysqli class, which replaces the set of legacy MySQL functions.

To connect to MySQL using the MySQL Improved extension, follow these steps:

  1. Use the following PHP code to connect to MySQL and select a database. Replace username with your username, password with your password, and dbname with the database name:
    <?php
        $mysqli = new mysqli("localhost", "username", "password", "dbname");
    ?>
    
  2. After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the employees table, and stores the result in the $result variable:

    <?php
        $result = $mysqli->query("SELECT lastname FROM employees");
    ?>
    

CONNECTING TO MYSQL USING PDO (PHP DATA OBJECTS)

The MySQL Improved extension can only be used with MySQL databases. PDO, on the other hand, abstracts database access and enables you to create code that can handle different types of databases.

To connect to MySQL using PDO, follow these steps:

  1. Use the following PHP code to connect to MySQL and select a database. Replace username with your username, password with your password, and dbname with the database name:
    <?php
        $myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');
    ?>
    
  2. After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the employees table, and stores the result in the $result variable:

    <?php
        $result = $myPDO->query("SELECT lastname FROM employees");
    ?>
    

CONNECTING TO MYSQL USING THE LEGACY MYSQL FUNCTIONS

The original PHP MySQL functions (whose names begin with mysql_) are deprecated in PHP 5.5, and will eventually be removed from PHP. Therefore, you should only use these functions when absolutely necessary for backward compatibility. If possible, use the MySQL Improved extension or PDO instead.

To connect to MySQL using the legacy PHP MySQL functions, follow these steps:

  1. Use the following PHP code to connect to MySQL and select a database. Replace username with your username, password with your password, and dbname with the database name:
    <?php
        mysql_connect('localhost','username','password');
        mysql_select_db("dbname");
    ?>
    
  2. After the code connects to MySQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the employees table, and stores the result in the $result variable:

    <?php
        $result = mysql_query('SELECT lastname FROM employees');
    ?>

 

Was this answer helpful?

 Print this Article

Also Read

cPanel Guide

Overview This document explains how to log into cPanel, WHM, Webmail, and the Command Line...

Why should I choose OneServicePlace Hosting?

Why should I choose OneServicePlace Hosting? We know there is a lot of competition in the web...

How to access e-mail accounts through webmail

WHAT IS WEBMAIL? Webmail applications enable you to check your e-mail accounts by using your web...

WordPress Optimization

How to optimize WordPress with W3 Total Cache and GTmetrix As your site gains traffic and users,...

How to access e-mail accounts using client applications

This article describes how to configure a client e-mail application so it can access an e-mail...

Powered by WHMCompleteSolution