MySQL common functions in PHP (PHP under the operation of the database required) _php Foundation
Source: Internet
Author: User
1, mysql_connect ()-Establish a database connection Format:
Resource mysql_connect ([string hostname [:p ort] [:/path/to/socket] [, string username] [, string password]])
Cases:
$conn = @mysql_connect ("localhost", "username", "password") or Die ("Cannot connect to MySQL Server");
Description: The close connection that you must display with this connection
2, Mysql_pconnect ()-Establish a database connection Format:
Resource Mysql_pconnect ([string hostname [:p ort] [:/path/to/socket] [, string username] [, string password]])
Cases:
$conn = @mysql_pconnect ("localhost", "username", "password") or dir ("Cannot connect to MySQL Server");
Note: The connection function does not need to be shown to close the connection, it is equivalent to using a connection pool
3, Mysql_close ()-Close the database connection Cases:
$conn = @mysql_connect ("localhost", "username", "password") or Die ("Cannot connect to MySQL Server");
@mysql_select_db ("MyDatabase") or Die ("cannot select this database, or the database does not exist");
echo "You have connected to the MyDatabase database";
Mysql_close ();
4, mysql_select_db ()-Select database Format:
Boolean mysql_select_db (String db_name [, Resource link_id])
Cases:
$conn = @mysql_connect ("localhost", "username", "password") or Die ("Cannot connect to MySQL Server");
@mysql_select_db ("MyDatabase") or Die ("cannot select this database, or the database does not exist");
5, mysql_query ()-Query MySQL Format:
Resource mysql_query (string query, [resource link_id])
Cases:
$linkId = @mysql_connect ("localhost", "username", "password") or Die ("Cannot connect to MySQL Server");
@mysql_select_db ("MyDatabase") or Die ("cannot select this database, or the database does not exist");
$query = "SELECT * from MyTable";
$result = mysql_query ($query);
Mysql_close ();
NOTE: If the SQL query executes successfully, the resource identifier is returned and false when it fails. Returns true if the update is successful, otherwise returns false
6, Mysql_db_query ()-Query MySQL Format:
Resource Mysql_db_query (string database, string query [, Resource link_id])
Cases:
$linkId = @mysql_connect ("localhost", "username", "password") or Die ("Cannot connect to MySQLServer");
$query = "SELECT * from MyTable";
$result = Mysql_db_query ("MyDatabase", $query);
Mysql_close ();
Description: To make the code clear, this function call is not recommended
7, Mysql_result ()-Get and display data Format:
Mixed mysql_result (resource result_set, int row [, mixed field])
Cases:
$query = "SELECT ID, name from MyTable order by name";
$result = mysql_query ($query);
for ($count =0; $count <=mysql_numrows ($result); $count + +)
{
$c _id = mysql_result ($result, 0, "id");
$c _name = mysql_result ($result, 0, "name");
echo $c _id, $c _name;
}
Description: The simplest and least efficient data acquisition function
8, Mysql_fetch_row ()-Get and display data Format:
Array mysql_fetch_row (Resource result_set)
Cases:
$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/>");
}
Description: The function gets the entire row of data from the Result_set and places the value in an indexed array. Usually knots make the list () function use
9, Mysql_fetch_array ()-Get and display data Format:
Array mysql_fetch_array (Resource result_set [, int result_type])
Cases:
$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/>";
}
Description
The Result_type values are:
MYSQL_ASSOC: Field name indicates key, field content is value
Mysql_num: Array of numeric indices, operating as Mysql_fetch_ros () functions
Mysql_both: is returned as an associative array and as a numeric index array. The default value of the Result_type.
10, Mysql_fetch_assoc ()-Get and display data Format:
Array Mysql_fetch_assoc (Resource result_set)
Equivalent to calling Mysql_fetch_array (resource, MYSQL_ASSOC);
11, Mysql_fetch_object ()-Get and display data Format:
Object Mysql_fetch_object (Resource Result_set)
Cases:
$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: Returns an object in the same operation as Mysql_fetch_array ()
12, Mysql_num_rows ()-The number of records selected Format:
int mysql_num_rows (Resource result_set)
Cases:
query = "SELECT ID, name from MyTable where ID > 65";
$result = mysql_query ($query);
echo "has". Mysql_num_rows ($result). " The ID of the record is greater than 65 ";
Description: Only useful when determining the number of records obtained by a select query.
13, Mysql_affected_rows ()-The number of records affected by Insert,update,delete Format:
int Mysql_affected_rows ([resource link_id])
Cases:
$query = "Update MyTable set name= ' Cheneyfu ' where id>=5";
$result = mysql_query ($query);
The number of records with the echo "id greater than or equal to 5 is updated:". Mysql_affected_rows ();
Description: This function gets the number of rows affected by the Insert,update or delete update statement
14, Mysql_list_dbs ()-Get Database list information Format:
Resource Mysql_list_dbs ([Resource link_id])
Cases:
mysql_connect ("localhost", "username", "password");
$dbs = Mysql_list_dbs ();
echo "Databases: <br/>";
while (list ($db) = Mysql_fetch_rows ($dbs)) {
echo "$db <br/>";
}
Description: Displays all database names
15, Mysql_db_name ()-Get database name Format:
String Mysql_db_name (Resource Result_set, integer index)
Description: This function gets the name of the database at the specified index index in the returned result_set of Mysql_list_dbs ()
16, Mysql_list_tables ()-Get the list of database tables Format:
Resource Mysql_list_tables (String database [, Resource link_id])
Cases:
mysql_connect ("localhost", "username", "password");
$tables = Mysql_list_tables ("MyDatabase");
while (list ($table) = Mysql_fetch_row ($tables)) {
echo "$table <br/>";
}
Description: This function gets the table name of all tables in the database
17, Mysql_tablename ()-Get a database table name Format:
String Mysql_tablename (Resource Result_set, integer index)
Cases:
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 gets the table name at the specified index index in the returned result_set of Mysql_list_tables ()
18, Mysql_fetch_field ()-Get field information Format:
Object Mysql_fetch_field (resource result [, int field_offset])
Cases:
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>";
}
Description
A total of 12 object properties were returned for the object:
Name: Field name
Table: Tables in which the fields are located
Max_length: Maximum length of field
Not_null: 1 If the field cannot be null, otherwise 0
Primary_key: 1 If the field is the primary key, or 0
Unique_key: 1 If the field is a unique key, or 0
Multiple_key: 1 If the field is not unique, or 0
Numeric: If the field is a numeric value of 1, otherwise 0
Blob: If the field blob is 1, otherwise 0
Type: Data type of field
Unsigned: 1 If the field is unsigned, or 0
Zerofill: If the field is "0 fills" then 1, otherwise 0
19, Mysql_num_fields ()-Get the number of fields in the query Format:
Integer mysql_num_fields (Resource result_set)
Cases:
$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 ()-Gets the field names of all the fields in the specified table Format:
Resource Mysql_list_fields (string database_name, string table_name [, Resource link_id])
Cases:
$fields =mysql_list_fields ("MyDatabase", "MyTable");
The number of fields in the table MyTable in the Echo database mydatabase: ". Mysql_num_fields ($fields)." <br/> ";
21, Mysql_field_flags ()-Get the specified field options Format:
String Mysql_field_flags (Resource Result_set, integer field_offset)
Cases:
$query = "SELECT ID, name from MyTable order by name";
$result = mysql_query ($query);
$row =mysql_fetch_wor ($row);
22, Mysql_field_len ()-Gets the maximum length of the specified field Format:
Integer Mysql_field_len (Resource result_set, integer field_offset)
Cases:
$query = "SELECT name from MyTable";
$result = mysql_query ($query);
$row = Mysql_fetch_row ($result);
Echo Mysql_field_len ($result, 0). " <br/> ";
Description
If Mysql_field_len ($reseult, 0) = 16777215
Then Numer_format (Mysql_field_len ($result)) equals 16,777,215
23, Mysql_field_name ()-Get field name Format:
String Mysql_field_name (Resource result_set, int field_offset)
Cases:
$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 ()-Get field type Format:
String Mysql_field_type (Resource result_set, int field_offset)
Cases:
$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 ()-Get the table name of the field Format:
String mysql_field_table (Resource result_set, int field_offset)
Cases:
$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
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