PHP mysql_query () function

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.