MySQL Related instructions _php basics

Source: Internet
Author: User
Tags constant mysql client mysql functions mysql host mysql query
Resource type
Two resource types are used in the MySQL module. The first is the connection handle for the database and the second is the result set returned by the SQL query.

Predefined constants
The following constants are defined by this extension module, so it is only valid if this extension is compiled into PHP or dynamically loaded at run time.

In later versions of PHP 4.3.0, additional client tags are allowed in the mysql_connect () function and the Mysql_pconnect () function. The constants defined are listed below:

Table 2. MySQL Client Constants

Constant description
Mysql_client_compress Use compressed Communication protocol
Mysql_client_ignore_space allows spaces to be left blank after function name
Mysql_client_interactive allows you to set the Interactive_timeout time (in lieu of wait_timeout) that you are waiting to be idle before disconnecting.
MYSQL_CLIENT_SSL uses SSL encryption. This flag is available only when the MySQL client library version is 4.x or later. The PHP 4 and Windows version of the PHP 5 installation package are bound to 3.23.x.


The Mysql_fetch_array () function uses a constant to represent the type of the returned array. The following are the definitions of constants:

Table 3. MySQL Fetch Constants

Constant description
The data column returned by MYSQL_ASSOC uses the name of the index of the field for the array.
The data column returned by Mysql_both uses the field name and numeric index as the index name of the array.
The data column returned by Mysql_num uses a numeric index as the index name of the array. The index starts at 0, representing the first field that returns the result.


Comments
Note: Most MySQL functions accept Link_identifier as the last optional parameter. If this parameter is not provided, the last open connection is used. If no connection exists, the default parameters defined in PHP.ini are used to attempt to establish the connection. If the connection is unsuccessful, the function returns FALSE.

Example
The following simple example demonstrates how to connect to a database, execute a query statement, print a series of basic MySQL operations, such as returning a result set and disconnecting a database. Example 1. MySQL Example

<?php
Connect, select Database
$link = mysql_connect (' mysql_host ', ' mysql_user ', ' Mysql_password ')
Or Die (' could not connect: '. Mysql_error ());
Echo ' Connected successfully ';
mysql_select_db (' my_database ') or die (' could not select database ');

Execute SQL Query
$query = ' Select * from my_table ';
$result = mysql_query ($query) or Die (' Query failed: '. mysql_error ());

Display results in HTML
echo "<table>\n";
while ($line = Mysql_fetch_array ($result, Mysql_assoc)) {
echo "\t<tr>\n";
foreach ($line as $col _value) {
echo "\t\t<td> $col _value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";

Releasing the result set
Mysql_free_result ($result);

Close connection
Mysql_close ($link);
?>



Directory
Mysql_affected_rows-the number of record rows affected by the previous MySQL operation
Mysql_change_user--Changing the user who is logged on in the active connection
Mysql_client_encoding--Returns the name of the character set
Mysql_close--Close MySQL connection
Mysql_connect--Open a connection to the MySQL server
mysql_create_db--Create a new MySQL database
Mysql_data_seek--A pointer to moving internal results
Mysql_db_name--Get the result data
Mysql_db_query--Send a MySQL query
mysql_drop_db--Discard (delete) a MySQL database
Mysql_errno--numeric code that returns the error message from the previous MySQL operation
Mysql_error-Returns the text error message from the previous MySQL operation
Mysql_escape_string--Escapes a string for mysql_query
Mysql_fetch_array--Take one row from the result set as an associative array, or an array of numbers, or both
MYSQL_FETCH_ASSOC--get one row from the result set as an associative array
Mysql_fetch_field--Get column information from the result set and return as an object
Mysql_fetch_lengths--Gets the length of each output in the result set
Mysql_fetch_object--get one row from the result set as an object
Mysql_fetch_row--get one row from the result set as an enumeration array
Mysql_field_flags--flags that are associated with the specified field from the result
Mysql_field_len--Returns the length of the specified field
Mysql_field_name--Gets the field name of the specified field in the result
Mysql_field_seek--Sets the pointer in the result set to the defined field offset
Mysql_field_table--Gets the name of the table that contains the specified field
Mysql_field_type--Gets the type of the specified field in the result set
Mysql_free_result--releasing the resulting memory
Mysql_get_client_info--Get MySQL client information
Mysql_get_host_info--Get MySQL host information
Mysql_get_proto_info--Get MySQL protocol information
Mysql_get_server_info--Get MySQL server information
Mysql_info--Get the latest query information
MYSQL_INSERT_ID--Gets the ID generated by the previous insert operation
Mysql_list_dbs--List all the databases in the MySQL server
Mysql_list_fields--Lists the fields in the MySQL results
Mysql_list_processes--Listing the MySQL process
Mysql_list_tables--List the tables in the MySQL database
Mysql_num_fields--Gets the number of fields in the result set
Mysql_num_rows--Number of rows in the result set
Mysql_pconnect--Open a persistent connection to the MySQL server
Mysql_ping-Ping a server connection and reconnect if there is no connection
Mysql_query--Send a MySQL query
Mysql_real_escape_string--A special character in the string used in an escape SQL statement, taking into account the current character set of the connection
Mysql_result--Get the result data
mysql_select_db--Choose MySQL Database
Mysql_stat--Get the current system state
Mysql_tablename--Get table name
MYSQL_THREAD_ID--Returns the ID of the current thread
Mysql_unbuffered_query-Send a SQL query to MySQL without getting and caching the results of the row
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.