MySQL common functions in PHP
1. mysql_connect ()-create a database connection
Format:
Resource mysql_connect ([String hostname [: Port] [:/path/to/socket] [, string username] [, string Password])
Example:
$ Conn = @ mysql_connect ("localhost", "username", "password") or die ("cannot connect to MySQL Server ");
Note: Close the connection that must be displayed when this connection is used
2. mysql_pconnect ()-create a database connection
Format:
Resource mysql_pconnect ([String hostname [: Port] [:/path/to/socket] [, string username] [, string Password])
Example:
$ Conn = @ mysql_pconnect ("localhost", "username", "password") or Dir ("cannot connect to MySQL Server ");
Note: To use this connection function, you do not need to display the closed connection. It is equivalent to using the connection pool.
3. mysql_close ()-close the database connection
Example:
$ Conn = @ mysql_connect ("localhost", "username", "password") or die ("cannot connect to MySQL Server ");
@ Mysql_select_db ("mydatabase") or die ("this database cannot be selected, or the database does not exist ");
Echo "you have connected to mydatabase ";
Mysql_close ();
4. mysql_select_db ()-select a database
Format:
Boolean mysql_select_db (string db_name [, resource link_id])
Example:
$ Conn = @ mysql_connect ("localhost", "username", "password") or die ("cannot connect to MySQL Server ");
@ Mysql_select_db ("mydatabase") or die ("this database cannot be selected, or the database does not exist ");
5. mysql_query ()-query mysql
Format:
resource mysql_query (string query, [Resource link_id])
example:
$ linkid = @ mysql_connect ("localhost", "username", "password") or die ("cannot connect to MySQL Server ");
@ mysql_select_db ("mydatabase") or die ("this database cannot be selected, or the database does not exist");
$ query = "select * From mytable ";
$ result = mysql_query ($ query);
mysql_close ();
Description: If the SQL query is executed successfully, the resource identifier is returned. If the query fails, false is returned. If the update is successful, true is returned; otherwise, false is returned.
6. mysql_db_query ()-query mysql
Format:
resource mysql_db_query (string database, string Query [, resource link_id])
example:
$ linkid = @ mysql_connect ("localhost", "username", "password") or die ("cannot connect to mysqlserver ");
$ query = "select * From mytable";
$ result = mysql_db_query ("mydatabase", $ query);
mysql_close ();
Note: to make the Code clearer, this function call is not recommended.
7. mysql_result ()-obtain and display data
Format:
mixed mysql_result (resource result_set, int row [, mixed field])
example:
$ query = "select ID, name from mytable order by name";
$ result = mysql_query ($ query);
for ($ COUNT = 0; $ count <= mysql_numrows ($ result); $ count ++)
{< br> $ c_id = mysql_result ($ result, 0, "ID ");
$ c_name = mysql_result ($ result, 0, "name");
echo $ c_id, $ c_name;
}< br> description: simplest and least efficient data acquisition function
8. mysql_fetch_row ()-Get and display data
Format:
Array mysql_fetch_row (resource result_set)
Example:
$ Query = "select ID, name from mytable order by name ";
$ Result = mysql_query ($ query );
While (List ($ id, $ name) = mysql_fetch_row ($ result )){
Echo ("Name: $ name ($ id) <br/> ");
}
Note: The function retrieves the entire data row from result_set and places the value in an index array. Usually, the List () function is used.
9. mysql_fetch_array ()-Get and display data
Format:
Array mysql_fetch_array (resource result_set [, int result_type])
Example:
$ Query = "select ID, name from mytable order by name ";
$ Result = mysql_query ($ query );
While ($ ROW = mysql_fetch_array ($ result, mysql_assoc )){
$ Id = $ row ["ID"];
$ Name = $ row ["name"];
Echo "Name: $ name ($ id) <br/> ";
}
Another example:
$ Query = "select ID, name from mytable order by name ";
$ Result = mysql_query ($ query );
While ($ ROW = mysql_fetch_array ($ result, mysql_num )){
$ Id = $ row [0];
$ Name = $ row [1];
Echo "Name: $ name ($ id) <br/> ";
}
Note:
Result_type has the following values:
Mysql_assoc: field name indicates a key, and the field content is a value
Mysql_num: numeric index array. The operation is the same as the mysql_fetch_ros () function.
Mysql_both: it is returned as an associated array and a numerical index array. The default value of result_type.
10. mysql_fetch_assoc ()-obtain and display data
Format:
Array mysql_fetch_assoc (resource result_set)
Call mysql_fetch_array (resource, mysql_assoc );
11. mysql_fetch_object ()-obtain and display data
Format:
Object mysql_fetch_object (resource result_set)
Example:
$ Query = "select ID, name from mytable order by name ";
While ($ ROW = mysql_fetch_object ($ result )){
$ Id = $ row-> ID;
$ Name = $ row-> name;
Echo "Name: $ name ($ id) <br/> ";
}
Description: an object is returned, which is the same as mysql_fetch_array () in operation.
12. mysql_num_rows ()-number of records selected
Format:
Int mysql_num_rows (resource result_set)
Example:
Query = "select ID, name from mytable where ID> 65 ";
$ Result = mysql_query ($ query );
Echo "has". mysql_num_rows ($ result). "The Record ID is greater than 65 ";
Note: It is only useful when determining the number of records obtained by the SELECT query.
13. mysql_affected_rows ()-number of records affected by insert, update, and delete
Format:
Int mysql_affected_rows ([Resource link_id])
Example:
$ Query = "Update mytable set name = 'cheneyfu' where ID> = 5 ";
$ Result = mysql_query ($ query );
Echo "number of records whose ID is greater than or equal to 5:". mysql_affected_rows ();
Description: This function obtains the number of rows affected by the insert, update, or delete update statement.
14. mysql_list_dbs ()-obtain database list information
Format:
Resource mysql_list_dbs ([Resource link_id])
Example:
Mysql_connect ("localhost", "username", "password ");
$ DBS = mysql_list_dbs ();
Echo "databases: <br/> ";
While (List ($ dB) = mysql_fetch_rows ($ DBS )){
Echo "$ db <br/> ";
}
Description: displays the names of all databases.
15. mysql_db_name ()-obtain the Database Name
Format:
String mysql_db_name (resource result_set, integer index)
Description: This function is used to obtain the database name in the specified index in result_set returned by mysql_list_dbs ().
16. mysql_list_tables ()-obtain the database table list
Format:
Resource mysql_list_tables (string database [, resource link_id])
Example:
Mysql_connect ("localhost", "username", "password ");
$ Tables = mysql_list_tables ("mydatabase ");
While (List ($ table) = mysql_fetch_row ($ tables )){
Echo "$ table <br/> ";
}
Description: This function obtains the names of all tables in the database.
17. mysql_tablename ()-obtain the name of a database table
Format:
String mysql_tablename (resource result_set, integer index)
Example:
Mysql_connect ("localhost", "username", "password ");
$ Tables = mysql_list_tables ("mydatabase ");
$ COUNT =-1;
While (++ $ count <mysql_numrows ($ tables )){
Echo mysql_tablename ($ tables, $ count). "<br/> ";
}
Description: This function obtains the name of the table in the specified index in result_set returned by mysql_list_tables ().
18. mysql_fetch_field ()-obtain field information
Format:
Object mysql_fetch_field (resource result [, int field_offset])
Example:
Mysql_connect ("localhost", "username", "password ");
Mysql_select_db ("mydatabase ");
$ Query = "select * From mytable ";
$ Result = mysql_query ($ query );
$ Counts = mysql_num_fields ($ result );
For ($ COUNT = 0; $ count <$ counts; $ count ++ ){
$ Field = mysql_fetch_field ($ result, $ count );
Echo "<p> $ field-> name $ field-> type ($ field-> max_length) </P> ";
}
Note:
The returned object has 12 object attributes:
Name: field name
Table: The table where the field is located.
Max_length: Maximum length of a field
Not_null: if the field cannot be null, it is 1; otherwise, 0
Primary_key: if the field is a primary key, it is 1; otherwise, 0
Unique_key: if the field is a unique key, it is 1, otherwise 0
Multiple_key: if the field is not unique, it is 1; otherwise, 0
Numeric: if the field is a numerical value, it is 1; otherwise, it is 0.
BLOB: if the field is blob, it is 1; otherwise, it is 0.
Type: Data Type of the field
Unsigned: if the field is unsigned, it is 1; otherwise, it is 0.
Zerofill: if the field is "zero fill", it is 1; otherwise, it is 0.
19. mysql_num_fields ()-obtain the number of queried Fields
Format:
Integer mysql_num_fields (resource result_set)
Example:
$ Query = "select ID, name from mytable order by name ";
$ Result = mysql_query ($ query );
Echo "the number of fields in this query is:". mysql_num_fields ($ result). "<br/> ";
20. mysql_list_fields ()-obtains the field names of all fields in the specified table.
Format:
Resource mysql_list_fields (string database_name, string table_name [, resource link_id])
Example:
$ Fields = mysql_list_fields ("mydatabase", "mytable ");
Echo "Number of mytable fields in the database mydatabase:". mysql_num_fields ($ fields). "<br/> ";
21. mysql_field_flags ()-obtain the specified field options
Format:
String mysql_field_flags (resource result_set, integer field_offset)
Example:
$ Query = "select ID, name from mytable order by name ";
$ Result = mysql_query ($ query );
$ ROW = mysql_fetch_wor ($ row );
22. mysql_field_len ()-obtain the maximum length of the specified field
Format:
Integer mysql_field_len (resource result_set, integer field_offset)
Example:
$ Query = "Select name from mytable ";
$ Result = mysql_query ($ query );
$ ROW = mysql_fetch_row ($ result );
Echo mysql_field_len ($ result, 0). "<br/> ";
Note:
If mysql_field_len ($ reseult, 0) = 16777215
Then numer_format (mysql_field_len ($ result) is 16,777,215
23. mysql_field_name ()-obtain the field name
Format:
String mysql_field_name (resource result_set, int field_offset)
Example:
$ Query = "select ID as pkid, name from mytable order by name ";
$ Result = mysql_query ($ query );
$ ROW = mysql_fetch_row ($ result );
Echo mysql_field_name ($ result, 0); // result: pkid
24. mysql_field_type ()-obtain the field type
Format:
String mysql_field_type (resource result_set, int field_offset)
Example:
$ Query = "select ID, name from mytable order by name ";
$ Result = mysql_query ($ query );
$ ROW = mysql_fetch_row ($ result );
Echo mysql_field_type ($ result, 0); // result: int
25. mysql_field_table ()-obtain the name of the table where the field is located
Format:
String mysql_field_table (resource result_set, int field_offset)
Example:
$ Query = "select ID as pkid, name from mytable order by name ";
$ Result = mysql_query ($ query );
$ ROW = mysql_fetch_row ($ result );
Echo mysql_field_table ($ result, 0); // result: mytable
This articleArticleSource of PHP Forum original link: http://bbs.php.cn/thread-21651-1-1.html