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.
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.
The above lines will print "Query is successful" as the query is correct one. In the above lines replace this line
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 executionImmediately 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 recordsIf 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