MySQL common functions in PHP (PHP Operational Database Prerequisites) _php Tutorial

Source: Internet
Author: User
1. mysql_connect ()-Establish 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: A close connection must be displayed using the connection

2. Mysql_pconnect ()-Establish 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: Using this connection function does not require a closed connection to be displayed, it is equivalent to using a connection pool

3, Mysql_close ()-Close 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 ();
Description: If the SQL query execution succeeds, the resource identifier is returned, and false is returned on failure. Returns true if the update was performed successfully, otherwise 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, it is not recommended to use this function call

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)
");
}
Description: The function takes the entire data row from Result_set and places the value in an indexed 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])
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)
";
}
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)
";
}
Description
The values of the result_type are:
MYSQL_ASSOC: Field name denotes key, field content is value
Mysql_num: An array of numerically indexed operations, as with the Mysql_fetch_ros () function
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)
";
}
Description: Returns an object that is the same as mysql_fetch_array () on an operation

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 entry is greater than 65 ";
Description: Used only when determining the number of records obtained by the select query.

13, Mysql_affected_rows ()-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);
echo "id greater than or equal to 5 number of records updated:". Mysql_affected_rows ();
Description: The 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:
";
while (list ($db) = Mysql_fetch_rows ($dbs)) {
echo "$db
";
}
Description: Show all database names

15, Mysql_db_name ()-Get the 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 in the Result_set returned by Mysql_list_dbs ()

16, Mysql_list_tables ()-Get a 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
";
}
Description: This function gets the table names of all tables in 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). "
";
}
Description: This function gets the name of the table at the specified index in the Result_set returned by 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 "

$field->name $field->type ($field->max_length)

";
}
Description
The returned objects have a total of 12 object properties:
Name: Field name
Table: tables where fields are located
Max_length: Maximum length of a field
Not_null: 1 If the field cannot be null, otherwise 0
Primary_key: 1 If the field is a primary key, otherwise 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: 1 If the field is numeric, otherwise 0
BLOB: 1 If the field is a blob, otherwise 0
Type: The data type of the field
Unsigned: 1 If the field is unsigned, otherwise 0
Zerofill: 1 If the field is "0 padding", otherwise 0

19, Mysql_num_fields ()-Gets 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). "
";

20, Mysql_list_fields ()-Gets the field name of all 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");
echo "Number of fields in table MyTable in database mydatabase:". Mysql_num_fields ($fields). "
";

21, Mysql_field_flags ()-Gets 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). "
";
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 ()-Gets 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

http://www.bkjia.com/PHPjc/322386.html www.bkjia.com true http://www.bkjia.com/PHPjc/322386.html techarticle 1, mysql_connect ()-Establish database connection format: Resource mysql_connect ([string hostname [:p ort] [:/path/to/socket] [, String username ] [, String password]]) Example: $conn = @mys ...

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