Notes for learning PHP and MySQL interactive function tables

Source: Internet
Author: User
I have been studying PHP and MySQL recently. I feel that the interaction functions between PHP and MySQL are procedural. Of course, mysqli extensions, object-oriented functions, Java and C # are added, and PHP is written again, some do not adapt, and I feel that I have returned to the Age of C. I learned some functions today and recorded them so that I can refer to them when I forget them later.

Description Letter count Detailed description Description
Establish Database Connection Mysql_connect () Resource mysql_connect ([String hostname [: Port] [:/path/to/socket] [, string username] [, string Password])
Example: $ conn = @ mysql_connect ("localhost", "username", "password") or Dir ("cannot connect to MySQL Server ");
Close the connection that must be displayed when this connection is used
Establish Database Connection Mysql_pconnect () 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 ");
The connection function does not need to be displayed to close the connection. It is equivalent to using the connection pool.
Close database connection Mysql_close () $ 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 ();
 
Select Database Mysql_select_db () Boolean mysql_select_db (string db_name [, resource link_id])
$ 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 ");
 
Query MySQL Mysql_query () Resource mysql_query (string query, [Resource link_id])
$ 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 ();
If the SQL query is successful, the resource identifier is returned. If the query fails, false is returned. If the update is successful, true is returned; otherwise, false is returned.
Query MySQL Mysql_db_query () Resource mysql_db_query (string database, string Query [, resource link_id])
$ Linkid = @ mysql_connect ("localhost", "username", "password") or die ("cannot connect to mysqlserver ");
$ Query = "select * From mytable ";
$ Result = mysql_db_query ("mydatabase", $ query );
Mysql_close ();
This function is not recommended for code clarity.
Obtain and display data Mysql_result () Mixed mysql_result (resource result_set, int row [, mixed field])
$ Query = "select ID, name from mytable order by name ";
$ Result = mysql_query ($ query );
$ C_id = mysql_result ($ result, 0, "ID ");
$ C_name = mysql_result ($ result, 0, "name ");
Simplest and least efficient Data Acquisition Function
Obtain and display data Mysql_fetch_row () Array mysql_fetch_row (resource result_set)
$ 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/> ");
}
The function retrieves the entire data row from result_set and places the value in an index array. Usually, the List () function is used.
Obtain and display data Mysql_fetch_array () Array mysql_fetch_array (resource result_set [, int result_type])
$ 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/> ";
}
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.
Obtain and display data Mysql_fetch_assoc () Array mysql_fetch_assoc (resource result_set)
Call mysql_fetch_array (resource, mysql_assoc );
 
Obtain and display data Mysql_fetch_object () Object mysql_fetch_object (resource result_set)
$ 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/> ";
}
The operation is the same as mysql_fetch_array ().
Selected record Mysql_num_rows () Int mysql_num_rows (resource result_set)
# 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 ";
It is only useful when determining the number of records obtained by the SELECT query.
Affected records Mysql_affected_rows () Int mysql_affected_rows ([Resource link_id])
$ 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 ();
This function obtains the number of rows affected by the insert, update, or delete update statements.
Obtain database list information Mysql_list_dbs () Resource mysql_list_dbs ([Resource link_id])
Mysql_connect ("localhost", "username", "password ");
$ DBS = mysql_list_dbs ();
Echo "databases: <br/> ";
While (List ($ dB) = mysql_fetch_rows ($ DBS )){
Echo "$ db <br/> ";
}
 
Get Database Name Mysql_db_name () String mysql_db_name (resource result_set, integer index) This function is used to obtain the database name that is located in the specified index in the result_set returned by mysql_list_dbs ().
Obtain the database table list Mysql_list_tables () Resource mysql_list_tables (string database [, resource link_id])
Mysql_connect ("localhost", "username", "password ");
$ Tables = mysql_list_tables ("mydatabase ");
While (List ($ table) = mysql_fetch_row ($ tables )){
Echo "$ table <br/> ";
}
This function obtains the names of all tables in the database.
Get database table name Mysql_tablename () String mysql_tablename (resource result_set, integer index)
Mysql_connect ("localhost", "username", "password ");
$ Tables = mysql_list_tables ("mydatabase ");
$ COUNT =-1;
While (++ $ count <mysql_numrows ($ tables )){
Echo mysql_tablename ($ tables, $ count). "<br/> ";
}
This function obtains the name of the table in the specified index in result_set returned by mysql_list_tables ().
Obtain Field Information Mysql_fetch_field () Object mysql_fetch_field (resource result [, int field_offset])
Mysql_connect ("localhost", "username", "password ");
Mysql_select_db ("mydatabase ");
$ Query = "select * From mytable ";
$ Result = mysql_query ($ query );
$ Fields = mysql_num_fields ($ result );
For ($ COUNT = 0; $ count <$ fieds; $ count ++ ){
$ Field = mysql_fetch_field ($ result, $ count );
Echo "<p> $ field-> name $ field-> type ($ field-> max_length) </P> ";
}
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.
Obtain the number of queried Fields Mysql_num_fields () Integer mysql_num_fields (resource result_set)
$ 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/> ";
Returns the number of fields in result_set.
Obtains the field names of all fields in the specified table. Mysql_list_fields () Resource mysql_list_fields (string database_name, string table_name [, resource link_id])
$ Fields = mysql_list_fields ("mydatabase", "mytable ");
Echo "Number of mytable fields in the database mydatabase:". mysql_num_fields ($ fields). "<br/> ";
 
Obtains the specified field options. Mysql_field_flags () String mysql_field_flags (resource result_set, integer field_offset)  
Obtains the maximum length of a specified field. Mysql_field_len () Integer mysql_field_len (resource result_set, integer field_offset)
$ Query = "Select name from mytable ";
$ Result = mysql_query ($ query );
$ ROW = mysql_fetch_row ($ result );
Echo mysql_field_len ($ result, 0). "<br/> ";
If mysql_field_len ($ reseult, 0) = 16777215
Then numer_format (mysql_field_len ($ result) is 16,777,215
Obtain the field name Mysql_field_name () String mysql_field_name (resource result_set, int field_offset)
$ 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
 
Obtain the field type Mysql_field_type () String mysql_field_type (resource result_set, int field_offset)
$ 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
 
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.