How to connect PHP with MySQL? (Procedural)

Share

To connect PHP with the MySQL database using the procedural method, we use the MySQLi extension. The extension allows us to access the functionality provided by MySQL 4.1. The mysqli extension AKA MySQL improved extension, was developed including new features found in MySQL systems versions 4.1.3 and newer.

Connecting MySQL Database using MySQLi extension:

<?php
$server = "localhost";
$username = "username";
$password = "password"; //your password

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

// Check connection
if (!$connection) {
  die("Connection error: " . mysqli_connect_error());
}
echo "Connection successfull";
?>

Understanding the above code :

$server : This variable holds the name of your server.

$username : This variable holds the username of your MySQL server. The default username for the XAMPP server is username.

$password: This variable holds the password for your MySQL server. The default password for the XAMPP is empty (” “)

$connection : This variable holds the value returned by the function mysqli_connect()

mysqli_connect(): Returns an object which represents the connection to a MySQL Server, or false on failure.

mysqli_connect_error(): Returns a string that describes the error. null is returned if no error occurred.

Some Important functions of MySQLi extension :

  • mysqli_close() – Returns the object which refers to connection close.


<?php
$server = "localhost";
$username = "incorrect username";
$password = "password"; //your password

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

// Check connection
if (!$connection) {
  die("Connection error: " . mysqli_connect_errno());
}
echo "Connection successfull";

mysqli_close($connection);
?>


  • mysqli_connect_errno() – Returns the error code from last connect call
<?php
$server = "localhost";
$username = "incorrect username";
$password = "password"; //your password

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

// Check connection
if (!$connection) {
  die("Connection error: " . mysqli_connect_errno());
}
echo "Connection successfull";
?>

# output : Connection error: 1045
  • mysqli_sqlstate() – Returns the SQLSTATE error from previous MySQL operation


<?php
$server = "localhost";
$username = "incorrect username";
$password = "password"; //your password

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

// Check connection
if (!$connection) {
  die("Connection error: " . mysqli_connect_errno());
}
echo "Connection successfull";


/* Table City already exists, so we should get an error */
if (!mysqli_query($connection, "CREATE TABLE City (ID INT, Name VARCHAR(30))")) {
    printf("Error - SQLSTATE %s.\n", mysqli_sqlstate($connection));
}

mysqli_close($connection);
?>

To explore more functions we refer to the official documentation and for SQL articles

Share
Sudeep Mishra

Sudeep Mishra

Healing

Leave a Reply

Your email address will not be published.

%d bloggers like this: