What functions does MySQL use to execute MySQL statements

Source: Internet
Author: User
Tags mysql query create database

What functions does MySQL use to execute MySQL statements

Definitions and usage

The mysql_query () function executes a MySQL query.

Grammar

mysql_query (query,connection) parameter description

Query required. Specify the SQL query to send. Note: The query string should not end with a semicolon.

Connection optional. Specify 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 the Select,show,explain or DESCRIBE statement and FALSE if the query executes incorrectly.

For other types of SQL statements, mysql_query () returns TRUE on successful execution, and returns FALSE when an error occurs.

A return value other than FALSE means that the query is legitimate and can be executed by the server. This does not indicate any number of rows that are affected or returned. It is quite possible that a query execution succeeded but did not affect or return any rows.

Tips and comments

Note: This function automatically reads and caches the recordset. Use Mysql_unbuffered_query () if you want to run a non-cached query.

Example 1

$con = mysql_connect ("localhost", "Mysql_user", "mysql_pwd");

if (! $con)

{

Die (' Could not connect: '. Mysql_error ());

}

$sql = "SELECT * from person";

mysql_query ($sql, $con);

Some code

Mysql_close ($con);

?>

Example 2

To create a new database by using the mysql_query () function:

$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 ();

}

?>

Related Article

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.