1. Establish and close the connection
1) mysql_connect ()
Resource mysql_connect ([string hostname [: port] [:/path/to/socket] [, string username] [, string password])
All parameters are optional.
Example:
@ Mysql_connect ("localhost", "user", "password ")
Or die ("cocould not connect to mysql server !");
Note: The @ symbol indicates any error message caused by a failed attempt. You will see the error message specified in die.
Note: When connecting to multiple mysql databases, you must specify the link ID of each connection, as shown below:
$ Link1 = @ mysql_connect ("server1", "user", "password ")
Or die ("cocould not connect to mysql server !");
$ Link2 = @ mysql_connect ("server2", "user", "password ")
Or die ("cocould not connect to mysql server !");
2) mysql_pconnect ()
Resource mysql_pconnect ([string hostname [: port] [:/path/to/socket] [, string username] [, string password])
Different from mysql_connect (), the existing link is first searched for and is created only when the link does not exist.
Note that you do not need to display the close connection (mysql_close (), because the connection will be placed in the pool, it is called a persistent connection.
3) mysql_close ()
Boolean mysql_close ([resource link_id])
Closing the connection is not required because it can be handled by mysql garbage collection.
If link_id is not specified, the latest link is closed.
2. Select a database
Mysql_select_db ()
Boolean mysql_select_db (string db_name [, resource link_id])
3. query MySql
1) mysql_query ()
Resource mysql_query (string query [, resource link_id])
Executes query.
2) mysql_db_query ()
Resource mysql_db_query (string database, string query [, resource link_id])
It is equivalent to mysql_select_db () + mysql_query (). It can be clearly seen from the parameters.
4. Get and display data
1) mysql_result ()
Mixed mysql_result (resource result_set, int row [, mixed field])
Obtains the data of a field from the specified row of result_set. This is simple but inefficient.
Example:
Copy codeThe Code is as follows: $ link1 = @ mysql_connect ("server1", "webuser", "password ")
Or die ("cocould not connect to mysql server !");
@ Mysql_select_db ("company") or die ("cocould not select database !");
$ Query = "select id, name from product order by name ";
$ Result = mysql_query ($ query );
$ Id = mysql_result ($ result, 0, "id ");
$ Name = mysql_result ($ result, 0, "name ");
Mysql_close ();
Note that the above Code is only the field value of the first data in the output result set. If you want to output all records, you need to process them cyclically.Copy codeThe Code is as follows :...
For ($ I = 0; $ I <= mysql_num_rows ($ result); $ I ++)
{
$ Id = mysql_result ($ result, 0, "id ");
$ Name = mysql_result ($ result, 0, "name ");
Echo "Product: $ name ($ id )";
}
...
Note: If the field name is an alias, the alias is used in mysql_result.
2) mysql_fetch_row ()
Array mysql_fetch_row (resource result_set)
Obtain the entire row from result_set and put the data into the array.
For example ):Copy codeThe Code is as follows :...
$ Query = "select id, name from product order by name ";
$ Result = mysql_query ($ query );
While (list ($ id, $ name) = mysql_fetch_row ($ result )){
Echo "Product: $ name ($ id )";
}
...
3) mysql_fetch_array ()
Array mysql_fetch_array (resource result_set [, int result_type])
Enhanced version of mysql_fetch_row.
Obtain each row of result_set as an associated array or/and a value index array.
Two types of arrays are obtained by default. You can set result_type:
MYSQL_ASSOC: returns the associated array, field name => Field Value
MYSQL_NUM: returns a numeric index array.
MYSQL_BOTH: obtains two types of arrays. Therefore, each field can be referenced by index offset or field name.
Example:Copy codeThe Code is as follows :...
$ Query = "select id, name from product order by name ";
$ Result = mysql_query ($ query );
While ($ row = mysql_fetch_array ($ result, MYSQL_BOTH )){
$ Name = $ row ['name']; // or $ name = $ row [1];
$ Name = $ row ['id']; // or $ name = $ row [0];
Echo "Product: $ name ($ id )";
}
...
4) mysql_fetch_assoc ()
Array mysql_fetch_assoc (resource result_set)
Equivalent to mysql_fetch_array ($ result, MYSQL_ASSOC)
5) mysql_fetch_object ()
Object mysql_fetch_object (resource result_set)
Like mysql_fetch_array (), the returned object is not an array.
Example:Copy codeThe Code is as follows :...
$ Query = "select id, name from product order by name ";
$ Result = mysql_query ($ query );
While ($ row = mysql_fetch_object ($ result )){
$ Name = $ row-> name;
$ Name = $ row-> id;
Echo "Product: $ name ($ id )";
}
...
5. Selected records and affected records
1) mysql_num_rows ()
Int mysql_num_rows (resource result_set)
Returns the number of rows in result_set.
Note: mysql_num_rows () is valid only when determining the number of records obtained by the select statement query. To obtain the number of records affected by the insert/updata/delete query, use mysql_affected_rows ().
2) mysql_affected_rows ()
Int mysql_affected_rows ([resource link_id])
Obtain the number of records affected by insert, updata, and delete queries.
Note: you do not need to enter a parameter. By default, the latest result of the recently established database connection is used. You can use the optional parameter link_id to select a database connection.
6. Obtain database and table information
1) mysql_list_dbs ()
Resource mysql_list_dbs ([resource link_id])
Obtain the names of all databases on the server.
Example:Copy codeThe Code is as follows: mysql_connect ("localhost", "name", "pwd ");
$ Dbs = mysql_list_dbs ();
While (list ($ db) = mysql_fetch_row (dbs )){
Echo "$ db <br> ";
}
Note that the output result is related to the user permissions used.
2) mysql_db_name ()
String mysql_db_name (resource result_set, interger index)
Obtain the database name whose position is index in result_set returned by mysql_list_dbs.
3) mysql_list_tables ()
Resource mysql_list_tables (string database [, resource link_id])
Obtain the names of all tables in the database.
4) mysql_tablename ()
String mysql_tablename (resource result_set, interger index)
Obtain the index table name in result_set returned by mysql_list_tables.
After learning the COM and. when using the Net (Windows) function, I found an example of using COM to operate SQL server and looked for relevant information. So I got this article about PHP ACCESS, I believe there are already many online websites. I suggest you paste them here.
My machine environment: WIN2000, APACHE2, PHP Version 5.1.0RC1Copy codeThe Code is as follows: <? Php
$ Conn = new COM ("ADODB. Connection") or die ("Cannot start ADODB. Connection ");
$ Conn-> Open ("Provider = Microsoft. Jet. OLEDB.4.0; Data Source = D: \ php5 \ netBook. mdb ");
$ Rs = $ conn-> Execute ("select * from manage"); // record set
$ Num_columns = $ rs-> Fields-> Count ();
Echo $ num_columns. "<br/> \ n ";
For ($ I = 0; $ I <$ num_columns; $ I ++ ){
$ Outputs [$ I] = $ rs-> Fields ($ I );
}
$ Rowcount = 0;
While (! $ Rs-> EOF ){
For ($ I = 0; $ I <$ num_columns; $ I ++)
{
Echo htmlspecialchars ($ response [$ I]-> value). "\ t ";
}
Echo "<br/> \ n ";
$ Rowcount ++; // rowcount auto-Increment
$ Rs-> MoveNext ();
}
$ Rs-> Close (); // Close the dataset
$ Conn-> Close ();
?>