PHP operation MySQL function detailed, MySQL and php interactive function _php Tutorial

Source: Internet
Author: User
1. Establish and close the connection
1) mysql_connect ()
Resource mysql_connect ([string hostname [:p ort][:/path/to/socket][,string username] [, string password]])
All parameters are optional
Example:
@mysql_connect ("localhost", "User", "password")
Or Die ("Could not connect to MySQL server!");
Note that the @ symbol represents any error message that is caused by a forbidden failed attempt, and the user will see the error message specified in die ().
Note that when connecting to multiple MySQL, you must specify the link ID for each connection, as follows:
$link 1 = @mysql_connect ("Server1″," "User", "password")
Or Die ("Could not connect to MySQL server!");
$link 2 = @mysql_connect ("Server2″," "User", "password")
Or Die ("Could not connect to MySQL server!");
2) Mysql_pconnect ()
Resource Mysql_pconnect ([string hostname [:p ort][:/path/to/socket][,string username] [, string password]])
Unlike mysql_connect (), the existing link is first found and is not created until it exists.
Note that you do not need to display a close connection (Mysql_close ()) because the connection will be placed in the pool, so it is called a persistent connection.
3) Mysql_close ()
Boolean mysql_close ([Resource link_id])
Closing a connection is not necessary because it can be handled by MySQL garbage collection.
If link_id is not specified, the closest 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])
Responsible for executing query.
2) Mysql_db_query ()
Resource Mysql_db_query (string database, string query [, Resource link_id])
is equivalent to mysql_select_db () + mysql_query (), which can be clearly seen from the parameters.
4. Acquiring and displaying data
1) mysql_result ()
Mixed mysql_result (resource result_set, int row [, mixed field])
Gets the data for a field from the specified row in the Result_set. Simple but inefficient.
Example:
Copy CodeThe code is as follows:
$link 1 = @mysql_connect ("Server1″," WebUser "," password ")
Or Die ("Could not connect to MySQL server!");
@mysql_select_db ("Company") or Die ("Could 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, and if you want to output all the records, you need to loop the processing.
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 that if the query field name is an alias, the alias is used in Mysql_result.
2) mysql_fetch_row ()
Array mysql_fetch_row (Resource result_set)
Gets the entire row from the Result_set and puts the data into the array.
Example (note and the smart fit of list):
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 ().
Gets each row of the Result_set as an associative array or/and a numeric index array.
By default, two arrays are obtained, result_type can be set:
MYSQL_ASSOC: Return associative array, field name = = Field value
Mysql_num: Returns an array of numeric indices.
Mysql_both: Gets the two arrays. Therefore each field can be referenced by index offset, or by 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)
As with the mysql_fetch_array () function, but instead of an array, an object is returned.
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 the Result_set.
Note that mysql_num_rows () is valid only if the number of records obtained by the SELECT statement query is determined, and mysql_affected_rows () is required if you want to get the number of records affected by the Insert/updata/delete query.
2) Mysql_affected_rows ()
int Mysql_affected_rows ([resource link_id])
Gets the number of records affected by the Insert/updata/delete query
Note that you do not need to enter parameters and use the most recent results from the most recently established database connection. You can select a database connection by using the optional parameter link_id.
6. Getting information about databases and tables
1) Mysql_list_dbs ()
Resource Mysql_list_dbs ([Resource link_id])
Gets all the database names 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
”;
}

Note that the output is related to the user rights that are used.
2) Mysql_db_name ()
String Mysql_db_name (Resource Result_set, Interger index)
Gets the name of the database where the position is index in the Result_set returned by Mysql_list_dbs ().
3) Mysql_list_tables ()
Resource Mysql_list_tables (String database [, Resource link_id])
Gets all the table names in database.
4) Mysql_tablename ()
String Mysql_tablename (Resource Result_set, Interger index)
Gets Mysql_list_tables () returns the name of the table in the result_set where the position is index.
In the learning of PHP com and. Net (Windows) functions, found an example of SQL Server through COM operation, find the relevant information, so there is this PHP connection to access article, I believe there are many online, or paste it here.
My machine Environment: win2000,apache2,php Version 5.1.0RC1
Copy CodeThe code is as follows:
$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. “
\ n ";
for ($i =0; $i < $num _columns; $i + +) {
$FLD [$i] = $rs->fields ($i);
}
$rowcount = 0;
while (! $rs->eof) {
for ($i =0; $i < $num _columns; $i + +)
{
Echo Htmlspecialchars ($fld [$i]->value). "\ t";
}
echo "
\ n ";
$rowcount + +; RowCount self-increment
$rs->movenext ();
}
$rs->close (); To close a data set
$conn->close ();
?>

http://www.bkjia.com/PHPjc/323308.html www.bkjia.com true http://www.bkjia.com/PHPjc/323308.html techarticle 1. Establish and close connection 1) mysql_connect () resource mysql_connect ([string hostname [:p ort][:/path/to/socket][,string username] [ , string Password]]) All parameters are optional examples ...

  • 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.