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.
It is recommended by PHP.NET to use PDO_MySQL or MySQLI extensions. The old MySQL extension was deprecated in PHP 5.5.0 and was removed in PHP 7
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.
<?Php
$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 "Query failed".
Error in query execution
Immediately after execution of mysql_query command we can print the error message 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.