SQL PHP HTML ASP JavaScript articles and free scripts to download

PHP MySQL Query Functions mysql_query()


This function mysql_query() is used to pass a sql query to mysql database. We can write any sql query like insert , select , update , delete etc and pass the query to mysql database. The result of the execution of the query will be known by monitoring the status. We will get true or false based on the success or failure status of the query. We can specify and connecting link as an optional parameter. If no connecting link is specified it assumes default connection.

We will use our student table which has fields as id, name, class and mark. Table name is student. You can download the sql dump file at that end of this tutorial for the student table.

Here it is assumed that mysql connection is open and we will discuss the query part only.

<?
$query="select name,class,mark from student";
$status=mysql_query($query) or die( "query failed");
if($status){echo "Query is successful";}
else {echo "Qyery failed";}
?>


The above lines will print "Query is successful" as the query is correct one. In the above lines replace this line

$query="select name,class,mark from student";

with

$query="select name,class1,mark from student";

Here we have intentionally written the field name as class1 ( actual field name is class). On execution of the query the value of $status will be FALSE so the echo command inside else statement will print "Qyery failed".

Error in query execution

Immediately after execution of mysql_query command we can print the error meessage if any to know the status of execution. You can read more on how to print mysql error message here.

Updating records

If we are using mysql_query to update or delete records then to know the status we can use mysql_affected_rows command to know how many records got affected by the query.

Download the SQL group by query file to create the table with data

Google+

More on PHP MySQL functions
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