PHP MySQL query with error printing

How to write SQL using PHP to handle the data in MySQL database? In any database driven script we have to update, add, modify, data in the tables. By using PHP we can do all this using different functions available in PHP. We will start with very basic function, which will execute any query written in sql and can be applied to MySQL database.


Structured Query Language or popularly known as SQL is an universal language to handle database. An introduction and different types of sql command like select, insert, update etc you will get in the sql section of this site. There are some advance SQL commands like left join, linking of tables etc to study. If you are not comfortable with SQL any time you can refer the materials in sql section.

There are three steps invoved in this process.
  1. Connection to database
  2. Build the query and execute
  3. Display the data
First ensure that you have established your mysql connection through PHP. To get the full details on php mysql connection you can read the article here. If you are using PDO then start with PDO connection string here.

PHP Functions & SQL

Let us start with the function required to execute one query in PHP. Once you have connection established then we can execute sql command by using PHP function mysql_query(). Here is the syntax of the function.

Let us first write the query and store in a variable. We will write a query to create table.

$query="CREATE TABLE student ( id int(2) NOT NULL auto_increment, name varchar(50) NOT NULL default '', class varchar(10) NOT NULL default '', mark int(3) NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM";

We have stored the sql create query in a variable $query and we will pass this as a parameter to the function like below.


The above command will execute the query ( stored in variable $query) and we can check the status of the query ( successful or not ) by checking the status of $rt. $rt will be true of the query is successfully executed or it will return false. We will use php if command to check the status of the query.

if($rt){echo " Command is successful ";}
{echo "Command is not successful ";}

So from the above line we can know that the query has worked or failed. But we will not come to know what the error is if the query has failed. To get the error message we have to use another function mysql_error() to print the error message returned by MySQL database after executing the query. Here it is how to print the error message.

echo mysql_error();

The above line will print the error returned by mysql database if the query fails to execute. You can read more on mysql error here.  

The complete code is available below.

$query="CREATE TABLE student4 ( id int(2) NOT NULL auto_increment, name varchar(50) NOT NULL default '',
class varchar(10) NOT NULL default '', mark int(3) NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM";
echo mysql_error();

if($rt){echo " Command is successful ";}
{echo " Command is not successful ";}

Above this code keep the code given at mysql connection page to keep the connection open

displaying records collected from MySQL table

Visitors Rating
Your Rating



Im getting a No Database Selected error when using this code. Im using PDO connection string linked aboe. Any Suggestions?


YOu need to connect to database and select database. Read the connection string link available here.
Ann Ekpe


Good day . please I need your assistance.I am currently designing a webpage with members signing in and having a dashboard.I want to be able to send data from the database to each particular member , so anytime they login to the website they can view the data... Urgent HELP


You can store the message in a table and display inside login area, that is when member login is completed this message can be displayed. The message can be managed by adding or updating new message etc.
Knowledge of any scripting language and Database handling is required.

Post Comment This is for short comments only. Use the forum for more discussions.

HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 All rights reserved worldwide Privacy Policy Disclaimer