PHP MySQL Query 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";
$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.

PHP MySQL functions mysql_num_rows() : Number of rows

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Thank you so much for giving me anwsers☺️

    Post your comments , suggestion , error , requirements etc here

    SQL Video Tutorials

    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer