SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP PHP & Database

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.

SQL

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.

$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.
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. 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";
$rt=mysql_query($query);
echo mysql_error();

if($rt){echo " Command is successful ";}
else
{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

Be the first to post comment on this article :


Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer