PHP connection to MySQL database Basics

Source: Internet
Author: User
Tags define function

BoolDefine(String name, mixed value [, bool case_insensitive])
The define function defines constants.

define('DB_HOST', 'localhost');define('DB_USER', 'root');define('DB_PWD', '123456');define('DB_NAME', 'mtest');

ResourceMysql_connect([String server [, string username [, string password [, bool new_link [, int client_flags])
The mysql_connect function is used to open or reuse a connection to the MySQL server.

$conn = mysql_connect('DB_HOST', 'DB_USER', 'DB_PWD');

ResourceMysql_query(String Query [, resource link_identifier])
The mysql_query function sends a query to the active database on the server associated with the specified connection identifier.
Mysql_query () Only returns a resource identifier for select, show, explain or describe statements. If the query execution is incorrect, false is returned. For other types of SQL statements, if mysql_query () is executed successfully, true is returned. If an error occurs, false is returned. If the return value is not false, the query is legal and can be executed by the server. This does not indicate any affected or returned number of rows. It is very likely that a query is successfully executed but does not affect or no rows are returned.

 $query = 'select * from message'; $result = mysql_query($query);

 

ArrayMysql_fetch_array(Resource result [, int result_type])
The mysql_fetch_array function is used to obtain a row from the result set as an associated array, or a number array, or both. If there are no more rows, returnFalse.
Mysql_fetch_array ($ result, mysql_num) ====== mysql_fetch_array ($ result)
Mysql_fetch_array ($ result, mysql_assoc) ======= mysql_fetch_assoc ($ result)

mysql_fetch_array($result, MYSQL_ASSOC)

BoolMysql_free_result(Resource result)
The mysql_free_result function is used to call mysql_free_result () only when considering how much memory will be occupied when a large result set is returned. After the script is completed, all associated memory will be automatically released. If the call succeeds, true is returned. If the call fails, false is returned.

mysql_free_result($result);

BoolMysql_close([Resource link_identifier])
The mysql_close function is used to close the non-persistent connection associated with the specified connection ID to the MySQL server. If link_identifier is not specified, the last opened connection is closed. Generally, mysql_close () is not required, because the opened non-persistent connection is automatically closed after the script is executed.

mysql_close($conn);

Example:

define('DB_HOST', 'localhost');define('DB_USER', 'root');define('DB_PWD', '123456');define('DB_NAME', 'mtest');$conn = mysql_connect('DB_HOST', 'DB_USER', 'DB_PWD');mysql_select_db(DB_NAME, $conn);mysql_query('SET NAMES UTF8');$query = 'select * from message';$result = mysql_query($query);mysql_fetch_array($result, MYSQL_ASSOC);mysql_free_result($result);mysql_close($conn);

 

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.