codescracker


php

PHP and MySQLi Connect to Database



« Previous Tutorial Next Tutorial »


To connect to MySQL database using PHP, use mysqli_connect() function.

PHP MySQLi Database Connection Syntax

Here is the general form of MySQL database connection using PHP with MySQLi.

$connectionVariable = mysqli_connect(serverName, serverUser, serverPassword, databaseName);

Here is the description of the above parameters used in connecting to the MySQL database using PHP:

PHP and MySQLi Database Connection Example

Here is an example shows how to connect to MySQL database using PHP with MySQLi.

<?php 
	// below code will connect to the MySQL database
	$conn = mysqli_connect('localhost', 'root', '', 'codescracker');
	// here codescracker is the name of the database that 
	// we have created earlier in previous tutorial
	if(mysqli_connect_error())
	{
		// error in database connection 
		// put 0 to count variable
		$count = 0;
	}
	else 
	{
		// database connection established successfully
		// therefore put 1 to count variable
		$count = 1;
	}
?>
<html>
<head>
	<title>PHP and MySQLi Connect to Database</title>
</head>
<body>
<?php
	if($count==1)
	{
		// display message that database connection established
		echo "<p>Connection to MySQL database using PHP with MySQLi
				established successfully.</p>";
	}
	else 
	{
		// display message that database connection is not established
		echo "<p>Error occurred while connecting to the database!</p>";
	}
?>
</body>
</html>

Here is the sample output produced by the above example of connecting to MySQL database using PHP.

php mysqli connect to database

If you want to check that the error message is working or not, then change the database name to an unknown database or any other that creates connection error to the MySQL database.

You can perform anything to create database connection error. Here we are going to replace the following code:

$conn = mysqli_connect('localhost', 'root', '', 'codescracker');

with

$conn = mysqli_connect('localhost', 'root', '', 'unknown');

You can also change server name, user, or password to create error in establishing the database connection.

After changing the code, here is the output that will produce on re-running the above PHP and MySQL example of connecting to the database.

php mysql database connection

If you don't want to see disturbing or irritating error messages, then add the following code at top of the example program of database connection using PHP with MySQLi.

error_reporting(1);

Now after adding the above code and re-running the example of database connection, you will now see the following output in your browser:

connect to database php mysql

You can also display the error message only, on changing some code in the above example. Here is the optimized version of the above example illustrating database connection using PHP with MySQLi.

<?php 
	error_reporting(1);
	$conn = mysqli_connect('localhost', 'root', '', 'unknown');
	if(mysqli_connect_error())
	{
		$count = 0;
		$errMsg = mysqli_connect_error();
	}
	else 
	{
		$count = 1;
	}
?>
<html>
<head>
	<title>PHP and MySQLi Connect to Database</title>
</head>
<body>
<?php
	if($count==1)
	{
		echo "<p>Connection to MySQL database using PHP with MySQLi
				established successfully.</p>";
	}
	else 
	{
		echo "<p>Error occurred while connecting to the database!</p>";
		echo "<p><b>Error Message: </b> ".$errMsg."</p>";
		echo "<p>Exiting...</p>";
		exit();
	}
?>
</body>
</html>

Here is the sample output produced by the above example of PHP and MySQLi connection to database.

connect to mysql database using php

« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test