SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

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.

Let us start with the function required to execute one query in PHP. Before that please 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. 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.

$rt=mysql_query($query);

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.

Related Tutorial
PHP MySQL Functions
PHP MySQL data display
if($rt){echo " Command is successful ";}

else {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.  

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";

$rt=mysql_query($query);
echo mysql_error();

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

Discuss this tutorial at forum


Further readings
PHP MySql Query
PHP MySql Data Display
Displaying records without repetitions of same field data
PHP Paging or breaking records per page
PHP MySQL Connection
Checking MySQL by phpinfo

 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.