Definition and usage
The mysql_query () function executes a MySQL query.
Grammar
mysql_query (query,connection)
| Parameters |
Description |
| Query |
Necessary. Specifies the SQL query to send. Note: The query string should not end with a semicolon. |
| Connection |
Optional. Specifies the SQL connection identifier. If not specified, the previous open connection is used. |
Description
If there is no open connection, this function attempts to call the mysql_connect () function without arguments to establish a connection and use it.
return value
mysql_query () returns a resource identifier only for Select,show,explain or DESCRIBE statements, or FALSE if the query is executed incorrectly.
For other types of SQL statements, mysql_query () returns TRUE on successful execution and returns FALSE on error.
A return value other than FALSE means that the query is legitimate and can be executed by the server. This does not indicate any information about the number of rows affected or returned. It is possible that a query executed successfully but did not affect or return any rows.
Hints and Notes
Note: The function automatically reads and caches the recordset. If you want to run a non-cached query, use Mysql_unbuffered_query ().
ExampleExample 1
<?php$con = mysql_connect ("localhost", "Mysql_user", "mysql_pwd"), if (! $con) {die (' Could not connect: ') mysql_error ( )); } $sql = "select * from"; mysql_query ($sql, $con);//Some code mysql_close ($con); >
Example 2
Create a new database with the mysql_query () function:
<?php$con = mysql_connect ("localhost", "Mysql_user", "mysql_pwd"), if (! $con) {die (' Could not connect: ') mysql_error ( )); } $sql = "CREATE DATABASE my_db", if (mysql_query ($sql, $con)) {echo "database my_db created"; }else {echo "Error Creating Database:". Mysql_error (); }?>
PHP MySQL functions
Other references:
http://php.net/manual/zh/function.mysql-query.php
PHP mysql_query () function