PHP MySQL Connection With MySQLi Example Code and mysqli_connect Function

How to Drop Table in phpMyAdmin

From the PHP script if you want to access the MySQL Database you have to first create a connection to the database server with the help of mysqli_connect mysqli function.

MySQL Examples in Both MySQLi and PDO Syntax

In this, and in the following chapters we demonstrate three ways of working with PHP and MySQL:

  • MySQLi (object-oriented)
  • MySQLi (procedural)
  • PDO

MySQLi Installation

For Linux and Windows: The MySQLi extension is automatically installed in most cases, when php5 mysql package is installed.

Open a Connection to MySQL

Before we can access data in the MySQL database, we need to be able to connect to the server:

Example – 1 MySQLi Procedural Example

`
 <?php
$server = "localhost";
$user = "root";
$password = "";

// Create connection
$connection1 = mysqli_connect($server, $user, $password);

// Check connection
if (!$connection1) {
    die("The Connection is failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?> 

In the above example we show that how you can create a connection between database server and php server.

PHP MySQL Connection
Fig.1 – PHP MySQL Connection.
You can leave a response, or trackback from your own site.
Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape