MySQL database function library

Source: Internet
Author: User

This function library is used to access the MySQL database. For MySQL details, and download MySQL database, please visit this Web site http://www.mysql.com. The Network also has a lot of user interface programs to process MySQL, it is recommended to download phpMyAdmin to the http://www.phpwizard.net/phpMyAdmin, you can use the browser to operate and manage MySQL. The entire phpMyAdmin program is completed with PHP3. You can also study the connection between PHP3 and MySQL.

Mysql_affected_rows: obtain the number of columns affected by the last MySQL operation.
Mysql_close: Close the MySQL server connection.
Mysql_connect: Open the MySQL server connection.
Mysql_create_db: Create a New MySQL database.
Mysql_data_seek: Move the internal return pointer.
Mysql_db_query: Send the query string to the MySQL database.
Mysql_drop_db: remove the database.
Mysql_errno: Error Code returned.
Mysql_error: error message returned.
Mysql_fetch_array: returns array data.
Mysql_fetch_field: obtains the field information.
Mysql_fetch_lengths: returns the maximum length of data in each column of a single column.
Mysql_fetch_object: Return class information.
Mysql_fetch_row: returns fields in a single column.
Mysql_field_name: return the name of the specified field.
Mysql_field_seek: configure a pointer to a field returned value.
Mysql_field_table: Obtain the name of the table for the current field.
Mysql_field_type: obtain the type of the current field.
Mysql_field_flags: Obtain the flag of the current field.
Mysql_field_len: Get the length of the current field.
Mysql_free_result: releases the returned memory usage.
Mysql_insert_id: returns the ID of the last INSERT command.
Mysql_list_fields: Used to list fields in a specified data table ).
Mysql_list_dbs: List available databases of the MySQL server ).
Mysql_list_tables: lists the tables of a specified database ).
Mysql_num_fields: gets the number of returned fields.
Mysql_num_rows: gets the number of returned columns.
Mysql_pconnect: enables persistent connection to the MySQL server.
Mysql_query: returns a query string.
Mysql_result: gets the query result.
Mysql_select_db: select a database.
Mysql_tablename: Get the table name.

Mysql_affected_rows
Obtain the number of columns affected by the last MySQL operation.
Syntax: int mysql_affected_rows (int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function obtains the number of columns (row) affected by INSERT, UPDATE, or DELETE in the last MySQL query. If the last query is DELETE and the WHERE command is not used, all data is deleted. This function returns 0. If SELECT is used at the end, the expected number will not be obtained by using this function, because it is valid only when you change the MySQL database function. To obtain the number of returned SELECT results, use the mysql_num_rows () function.

Mysql_close
Close the connection to the MySQL server.
Syntax: int mysql_close (int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function closes the connection to the MySQL database server. If the link_identifier parameter is not specified, the last connection is closed. If mysql_pconnect () is used for connection, this function cannot be used to close the connection. In fact, this function is not required. When the PHP full-page program ends, the non-permanent (non-persistent) connection to the database will be automatically closed. True is returned for success and false is returned for failure.
Reference: mysql_connect () mysql_pconnect ()

Mysql_connect
Open the MySQL server connection.
Syntax: int mysql_connect (string [hostname] [: port], string [username], string [password]);
Return Value: integer
Function Type: database functions
Description: This function establishes a connection with the MySQL server. All parameters can be omitted. When you use this function without adding any parameters, the default value of the hostname parameter is localhost, the default value of the username parameter is the owner of the PHP route execution, and the parameter password is a null string (that is, no password ). The hostname parameter can be followed by a colon and port number, indicating that the port is used to connect to MySQL. Of course, when using the database, you can use mysql_close () to turn off the connection earlier to save resources.
Example
This is an example provided by an anonymous user (18-Feb-1999)
<? Php
$ Dbh = mysql_connect ('localhost: 808080', 'mcclain', 'standard ');
Mysql_select_db ('admreqs ');
$ Query = "insert into requests (date, request, email, priority, status) values (NOW (), '$ description',' $ email ',' $ priority ', 'new ')";
$ Res = mysql_query ($ query, $ dbh );
$ Query = "select max (id) from requests ";
$ Res = mysql_query ($ query, $ dbh );
$ Err = mysql_error ();
If ($ err ){
Echo "An error occurred, please notify <a href = mailto: webmaster@my.site> webmaster </a> ";
}
$ Row = mysql_fetch_row ($ res );
Echo "your future number is:". $ row [0];
?>
Reference: mysql_close () mysql_pconnect ()

Mysql_create_db
Create a new MySQL database.
Syntax: int mysql_create_db (string database name, int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function is used to create a new database ). Before establishing a server, you must connect to the server.
Reference: mysql_drop_db ()

Mysql_data_seek
Move the internal return pointer.
Syntax: int mysql_data_seek (int result_identifier, int row_number );
Return Value: integer
Function Type: database functions
Description: This function moves the column pointer returned internally to the specified row_number. If mysql_fetch_row () is used, the value of the new column can be returned. Returns true if the call succeeds or false if the call fails.

Mysql_db_query
Send the query string to the MySQL database.
Syntax: int mysql_db_query (string database, string query, int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function is used to send a query string to the backend MySQL database. If the parameter link_identifier that can be omitted does not exist, the program will automatically find the connection code after other mysql_connect () connections. If an error occurs, false is returned. Otherwise, the returned code is returned.
Reference: mysql_connect ()

Mysql_drop_db
Remove a database.
Syntax: int mysql_drop_db (string database_name, int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function removes an existing database. Returns true if the call succeeds or false if the call fails.
Reference: mysql_create_db ()

Mysql_errno
Return Error code.
Syntax: int mysql_errno (int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function obtains the error code of the MySQL database server. It is usually used in the PHP Web page program development stage and used for debugging PHP and MySQL.
Example
<? Php
Mysql_connect ("marliesle ");
Echo mysql_errno (). ":". mysql_error (). "<BR> ";
Mysql_select_db ("nonexistentdb ");
Echo mysql_errno (). ":". mysql_error (). "<BR> ";
$ Conn = mysql_query ("SELECT * FROM nonexistenttable ");
Echo mysql_errno (). ":". mysql_error (). "<BR> ";
?>
Reference: mysql_error ()

Mysql_error
Returns an error message.
Syntax: string mysql_error (int [link_identifier]);
Return Value: String
Function Type: database functions
Description: This function obtains the error message of the MySQL database server. It is usually used in the development phase of PHP web programs and used together with mysql_errno () as the debugging and usage of PHP and MySQL.
Reference: mysql_errno ()

Mysql_fetch_array
Returns an array of data.
Syntax: array mysql_fetch_array (int result, int [result_typ]);
Returned value: Array
Function Type: database functions
Description: This function is used to split the query result into array variables. If the result contains no data, false is returned. This function can be called the enhancement function of mysql_fetch_row (). In addition to putting the returned column and numeric index into the array, you can also put the text index into the array. If multiple returned fields have the same text name, the last field is valid. The solution is to use a digital index or use the alias (alias) for these fields with the same name (column ). Note that the processing speed of using this function is not much slower than that of the mysql_fetch_row () function. You need to determine which function to use. The result_typ parameter is a constant value and has the following constants: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.
Example
<? Php
Mysql_connect ($ host, $ user, $ password );
$ Result = mysql_db_query ("database", "select * from table ");
While ($ row = mysql_fetch_array ($ result )){
Echo $ row ["user_id"];
Echo $ row ["fullname"];
}
Mysql_free_result ($ result );
?>

Mysql_fetch_field
Obtain field information.
Syntax: object mysql_fetch_field (int result, int [field_offset]);
Returned value: Class
Function Type: database functions
Description: The class information returned by this function is the result field (Column) information. The returned class attributes are as follows:
Name-field name
Table-name of the table in which the field belongs
Max_length-Maximum length of a field
Not_null-if it is 1, this field cannot be blank (null)
Primary_key-if it is 1, this field is the primary key)
Unique_key-if it is 1, this field is a unique key)
Multiple_key-if it is 1, this field is a Rewritable key (non-unique key)
Numeric-if the value is 1, this field is of the numerical type (numeric)
Blob-if it is 1, this field is of the bit type (BLOB)
Type-Field type
Unsigned-if it is 1, this field is unsigned)
If zerofill-is 1, this field is filled with zero-filled)
Reference: mysql_field_seek ()

Mysql_fetch_lengths
Returns the maximum data length of each column in a single column.
Syntax: array mysql_fetch_lengths (int result );
Returned value: Array
Function Type: database functions
Description: This function places the maximum length of each field in the last column processed by mysql_fetch_row () in an array variable. If the execution fails, false is returned. The first index of the returned array is 0.
Reference: mysql_fetch_row ()

Mysql_fetch_object
Class data.
Syntax: object mysql_fetch_object (int result, int [result_typ]);
Returned value: Class
Function Type: database functions
Description: This function is used to split the query result into class variables. The usage is almost the same as mysql_fetch_array (). The difference is that the data returned by this function is a class rather than an array. If the result contains no data, false is returned. In addition, it is important to note that the index of retrieved class data can only be text but cannot use numbers. This is because of the characteristics of the class. All the property names of the class data cannot be numbers, so you have to use text strings as indexes. The result_typ parameter is a constant value and has the following constants: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH. In terms of speed, the processing speed of this function is almost the same as that of mysql_fetch_row () and mysql_fetch_array (). It depends on the function to be used.
Example
The following example shows how to use the returned class.
<? Php
Mysql_connect ($ host, $ user, $ password );
$ Result = mysql_db_query ("MyDatabase", "select * from test ");
While ($ row = mysql_fetch_object ($ result )){
Echo $ row-> user_id;
Echo $ row-> fullname;
}
Mysql_free_result ($ result );
?>
Reference: mysql_fetch_array () mysql_fetch_row ()

Mysql_fetch_row
Return fields in a single column.
Syntax: array mysql_fetch_row (int result );
Returned value: Array
Function Type: database functions
Description: This function is used to split a single column of query result into array variables. The index of the array is a digital index, and the first index value is 0. If the result contains no data, false is returned.
Reference: mysql_fetch_array () mysql_fetch_object () mysql_data_seek () mysql_fetch_lengths () mysql_result ()

Mysql_field_name
Returns the name of the specified field.
Syntax: string mysql_field_name (int result, int field_index );
Return Value: String
Function Type: database functions
Description: This function is used to obtain the name of a specified field.
Example
Mysql_field_name ($ result, 2 );

Mysql_field_seek
Configure the pointer to return to a raised field.
Syntax: int mysql_field_seek (int result, int field_offset );
Return Value: integer
Function Type: database functions
Description: This function configures the current pointer to a specific field returned for the fundraising.
Reference: mysql_fetch_field ()

Mysql_field_table
Obtain the table name of the current field.
Syntax: string mysql_field_table (int result, int field_offset );
Return Value: String
Function Type: database functions
Description: This function obtains the name of the data table of the current field.

Mysql_field_type
Obtain the type of the current field.
Syntax: string mysql_field_type (int result, int field_offset );
Return Value: String
Function Type: database functions
Description: This function can obtain the Type format of the current field. The returned string is the type of the field, including int, real, string, blob... and so on. For details, see the related files of MySQL.
Example
<? Php
Mysql_connect ("localhost: 3306 ");
Mysql_select_db ("wisconsin ");
$ Result = mysql_query ("SELECT * FROM onek ");
$ Fields = mysql_num_fields ($ result );
$ Rows = mysql_num_rows ($ result );
$ I = 0;
$ Table = mysql_field_table ($ result, $ I );
Echo "data table '". $ table. "' has the". $ fields. "column and". $ rows. "column. <Br> ";
Echo "the fields in this table are as follows <br> ";
While ($ I <$ fields ){
$ Type = mysql_field_type ($ result, $ I );
$ Name = mysql_field_name ($ result, $ I );
$ Len = mysql_field_len ($ result, $ I );
$ Flags = mysql_field_flags ($ result, $ I );
Echo $ type. "". $ name. "". $ len. "". $ flags. "<br> ";
$ I ++;
}
Mysql_close ();
?>

Mysql_field_flags
Obtain the flag of the current field.
Syntax: string mysql_field_flags (int result, int field_offset );
Return Value: String
Function Type: database functions
Description: This function can be used to identify the current field. If a field has several attribute flags, the returned flags are the strings that these attributes are connected to. Each attribute is separated by a space. You can use explode () to cut these strings. The returned flag may be: not_null, primary_key, unique_key, multiple_key, blob, unsigned, zerofill, binary, enum, auto_increment, and timestamp.

Mysql_field_len
Obtain the length of the current field.
Syntax: int mysql_field_len (int result, int field_offset );
Return Value: integer
Function Type: database functions
Description: This function obtains the length of the current field.

Mysql_free_result
Release the returned memory usage.
Syntax: boolean mysql_free_result (int result );
Return Value: Boolean
Function Type: database functions
Description: This function can release the memory occupied by the query returned by the MySQL database. This function is generally used only when you are worried about insufficient memory usage. PHP programs will be automatically released at the end.

Mysql_insert_id
Returns the ID of the last INSERT command.
Syntax: int mysql_insert_id (int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function obtains the execution ID of the last INSERT operation to the MySQL database. Sleibowitz@btcwcu.org (13-May-1999) points out that using REPLACE in PHP 3.0.7 is the same as using INSERT, and you can use this function to get the ID.

Mysql_list_fields
Lists fields in a specified data table ).
Syntax: int mysql_list_fields (string database_name, string table_name, int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function obtains all fields in the specified data table. The returned field information can be used by functions such as mysql_field_flags (), mysql_field_len (), mysql_field_name (), and mysql_field_type. If an error exists,-1 is returned.

Mysql_list_dbs
List the databases available for the MySQL server ).
Syntax: int mysql_list_dbs (int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function can be used to obtain available databases of the MySQL server.

Mysql_list_tables
Lists the tables of a specified database ).
Syntax: int mysql_list_tables (string database, int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function obtains the names of all data tables in the specified database.

Mysql_num_fields
Obtain the number of returned fields.
Syntax: int mysql_num_fields (int result );
Return Value: integer
Function Type: database functions
Description: This function obtains the number of returned fields.
Reference: mysql_db_query () mysql_query () mysql_fetch_field () mysql_num_rows ()

Mysql_num_rows
Obtain the number of returned columns.
Syntax: int mysql_num_rows (int result );
Return Value: integer
Function Type: database functions
Description: This function returns the number of returned columns.
Reference: mysql_db_query () mysql_query () mysql_fetch_row ()

Mysql_pconnect
Enable the persistent connection of the MySQL server.
Syntax: int mysql_pconnect (string [hostname] [: port], string [username], string [password]);
Return Value: integer
Function Type: database functions
Description: This function is similar to mysql_connect. The difference is that when you use this function to open the database, the program first looks for whether the function has been executed. If it has been executed, the previous execution ID is returned. Another difference is that this function cannot use mysql_close () to close the database.

Mysql_query
Returns a query string.
Syntax: int mysql_query (string query, int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function sends a query string for MySQL to process or execute it. If the link_identifier parameter is not specified, the program automatically searches for the recently opened ID. When the query string is UPDATE, INSERT, or DELETE, the return value may be true or false. If the query string is SELECT, a new ID value is returned. The joey@samaritan.com (09-Feb-1999) states that when false is returned, the query string has an error instead of a successful execution but no return value.
Reference: mysql_db_query () mysql_select_db () mysql_connect ()

Mysql_result
Obtain the query result.
Syntax: int mysql_result (int result, int row, mixed field );
Return Value: integer
Function Type: database functions
Description: This function returns a query result. The field parameter can be in the field name, sequence, or FieldName. TableName format. This function can be used to process a small amount of data returned. When the database is large, the efficiency of this function needs to be considered. In this case, more efficient functions such as mysql_fetch_row (), mysql_fetch_array (), and mysql_fetch_object () can be used.

Mysql_select_db
Select a database.
Syntax: int mysql_select_db (string database_name, int [link_identifier]);
Return Value: integer
Function Type: database functions
Description: This function selects a database on the MySQL server for Data query Processing. True is returned for success, and false is returned for failure.
Reference: mysql_connect () mysql_pconnect () mysql_query ()

Mysql_tablename
Obtain the table name.
Syntax: string mysql_tablename (int result, int I );
Return Value: String
Function Type: database functions
Description: This function obtains the name string of the table. It is generally used with the mysql_list_tables () function to obtain the name string of the number returned by this function.
Example
<? Php
Mysql_connect ("localhost: 3306 ");
$ Result = mysql_list_tables ("wisconsin ");
$ I = 0;
While ($ I <mysql_num_rows ($ result )){
$ Tb_names [$ I] = mysql_tablename ($ result, $ I );
Echo $ tb_names [$ I]. "<BR> ";
$ I ++;
}
?>

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.