How to Connect to MySql database and run MySql query using PHP

2

Database is one of the very important issues in case of web development. As we all know, server site scripting is another important issue for different kinds of web development. In this article we will show how to connect mysql databases and how to run different mysql queries. As a server site scripting we will use PHP and how PHP connects to the database.

Before we show how to connect we need to know what are the prerequisite for connect to database. How to setup mysql into our server is shown in our different post. So we have mysql installed but we need to create a user to connect to database. We also can have root user as the default user created into our mysql database server. So let’s consider we have a test user created on our server with a password.

Let’s consider,

Username: testuser

Password: testuser123

Now we have to use some PHP code to connect with our database. The simple code is given below.

$server = “localhost”;
$user = ” testuser”;
$pwd = ” testuser123″;
//connect to mysql database
$con = mysqli_connect($server, $user, $pwd);
//mysql database connection status
if (!$con) {
die(“Connection did not estublished “);
}

echo “Connection successfull”;

here the first variable $server is our server address, $user is our user name and the last variable $pwd is our password. The function used here to establish connection is mysqli_connect. Once the mysql database connection is established and if we compare the connection using true or false condition then it will return true.

We will use this condition to make sure that the connection is true or not. For that we wrote the rest of the lines. In there we put the condition as false, if the condition is false then we will print “Connection did not estublished ” and break the program. Otherwise we will print “Connection successfull”.

So this above program is just as simple as that and we can create a mysql database connection using this simple program. Now if we want to run some mysql query using this program then what we need to do!

One the next part we will discuss in short about this.

To run a mysql query we have to construct that query into a text form within a variable. Then we have to put that variable into mysqli_query pseudo code.

Now suppose we will create a mysql database named testdatabase. So we have to construct a mysql database creating query and put that into a variable. We can do that as shown below.

$query = “CREATE DATABASE testdatabase”;

Now we have to put this query into mysqli_query function like shown below.

mysqli_query($con, $ query);

usually we would put this function into a condition to ensure that the database is created. So our final code will be life.

if (mysqli_query($con, $ query)) {
echo “Database is created”;
} else {
echo mysqli_error($con);
}

once our work is done we should close the mysql connection. For that we would run a simple function called mysqli_close.

so our final code will be like below.

$server = “localhost”;
$user = ” testuser”;
$pwd = ” testuser123″;
//connect to mysql database
$con = mysqli_connect($server, $user, $pwd);
//mysql database connection status
if (!$con) {
die(“Connection did not estublished “);
}

$query = “CREATE DATABASE testdatabase”;

if (mysqli_query($con, $ query)) {
echo “Database is created”;
} else {
echo mysqli_error($con);
}

mysqli_close($con);

like this query, we will show more queries on our future articles. The art of constructing new queries is the best part of this learning.

2 Comments
  1. Diksha says

    It is very helpful 👍

  2. Debby says

    Very informative!
    Thanks!

Leave A Reply

Your email address will not be published.