Execute Mysql Query Using PHP

Introduction :

In the previous lesson, you learned about different connection methods in PHP 5 to connect Mysql Database with PHP. Today on this article you are going to learn about an important function in PHP. This feature will allow you to execute Mysql Query using PHP. It is very important to know about PHP Mysql Query executing function because every program needs a Back-End and the program must be able to execute the Back-End using its code.

How to Execute Mysql Query in PHP?

It is very easy to Execute Mysql Query in PHP, To do this you must have minimum knowledge about creating the connection between Mysql Database and PHP first. If you don't know How to connect Database and PHP program, then click here.
  • First of all, you need to make a connection between your PHP program and Mysql Database, For that use the code which given below.
$con = @mysql_connect("localhost","root","password");
  • Don't forget to replace the "root" With your Mysql Database Username and "Password" With your Database password.
  • Now you need to select Database, For that you need to use the below PHP code.
$database = mysql_select_db("police");
  • Now you need to make a variable which contain the Mysql Query, Use the following code that given below. It is just an example Mysql Query to create a table. 
$query = "CREATE TABLE LOGIN (name varchar(50), class int(10))";
  • The variable "$query" Contain the Mysql Query to create a table named "Login" and some values in it. 
  • Now it times to execute the Mysql Query using PHP. 
$result = mysql_query($query, $con);
  • The above code will execute the Mysql Query which contained in the "$query" Using the "$con" Database connection and store to "$result" .
  • Now you need to take a look at the full example code and understand each and every step that I described above. 
$con = @mysql_connect("localhost","root","");
$database = mysql_select_db("police");
$query = "CREATE TABLE LOGIN (name varchar(50), class int(10))";
$result = mysql_query($query, $con);
 echo "Table Not Created " .mysql_error();
 echo "Table  created";
  • If Mysql Query successfully executed,  then you can see "Table Created" Message else you can see the "Table Not Created" Message and what the reason for not executing the query.

Suggested Article for PHP Developers:

  • Chapter 4: Insert Values to Database Table Using PHP.
  • Chapter 5: Fetch Data from Database Using PHP.
  • Chapter 6: Update Data of Database Table Using PHP.
I hope you well enjoyed this article. If you have any doubts or suggestions related to this article. Please let us know by using the comment box given below. Thanks


Post a Comment

Wapmaster or Webmaster can Comment Us with the following Comment Box . Don't add Links to your site for Spamming our Blog .. Once you add Comment with Link . It will be deleted immediatly by our Admins .