MySQL common functions in PHP (PHP operating database prerequisites)

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]]) Example: $conn = @mysql_connect ("localhost", "username", "password") or Die ("Cannot connect to MySQL Server"); Description: Use the connection must display the close connection 2, Mysql_pconnect ()-Establish database connection format: Resource mysql_pconnect ([string hostname [:p ort] [:/path/to/socket] [  , string Username] [, string password]]) Example: $conn = @mysql_pconnect ("localhost", "username", "password") or dir ("Cannot connect to MySQL Server ") Description: Use this connection function does not need to display the closed connection, it is equivalent to using connection Pool 3, mysql_close ()-Close the database connection example: $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]) Example: $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]) Example: $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: Returns the resource identifier if the SQL query execution succeeds, and returns false on failure. Returns true if the update was performed successfully, otherwise returns FALSE6, Mysql_db_query ()-Query MySQL format: Resource mysql_db_query (String database, string query [, Resource link_id]) Example: $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 ()-Gets and displays data format: Mixed mysql_result (Resource result_set, int row [, mixed field]) Example: $query = "SELECT ID, name from Mytab Le 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) Example: $query = "SELECT ID, name from MyTable order by name"; $re Sult = mysql_query ($query), while (List ($id, $name) = Mysql_fetch_row ($result)) {echo ("Name: $name ($id) <br/>");} Description: The function takes the entire data row from Result_set and places the value in an indexed array. Typically, the list () function is used with 9, mysql_fetch_array ()-Gets and displays the data format: Array mysql_fetch_array (Resource result_set [, int result_type]) Example: $  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) and while ($row = Mysql_fetch_array ($res Ult, mysql_num) {$id = $row [0]; $name = $row [1];echo "Name: $name ($id) <br/>";} Description: The value of the Result_type is: Mysql_assoc: The field name represents the key, the field content is the value mysql_num: The numeric index array, the operation is the same as the Mysql_fetch_ros () function Mysql_both: That is, as an associative array and returned 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) is 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) Example: $query = "SELECT ID, name from MyTable order by name", and while ($ro W = mysql_fetch_object ($result)) {$id = $row->id; $name = $row->name;echo "Name: $name ($id) <br/>";} Description: Returns an object that has the same operation as Mysql_fetch_array () 12, mysql_num_rows ()-The number of records selected in the format: int mysql_num_rows (Resource result_set) Example: query = "SELECT ID, name from MyTable where ID >"; $result = mysql_query ($query); echo "has". Mysql_num_rows ($result). " The ID of the record is greater than 65 "; Description: Only used when determining the number of records fetched by the select query. 13, Mysql_affected_rows ()-The number of records affected by Insert,update,delete format: int mysql_affected_rows ([resource link_id]) Example: $query = " Update MyTable set name= ' Cheneyfu ' where id>=5 '; $result = mysql_query ($query); echo "ID is greater than or equal to 5 the number of records that the name was 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 ()-Gets the database list Information Format: Resource Mysql_list_dbs ([ Resource link_id]) Example: mysql_connect ("localhost", "username", "Password "); $dbs = Mysql_list_dbs (); echo" Databases: <br/> "while (list ($db) = Mysql_fetch_rows ($dbs)) {echo $db & Lt;br/> ";} Description: Show all database names 15, Mysql_db_name ()-Gets the database name format: String mysql_db_name (Resource Result_set, integer index) Description: The function gets the Mysql_ List_dbs () returns the database name at the specified index index in Result_set 16, mysql_list_tables ()-Gets the Database table column table format: Resource Mysql_list_tables (string database [, Resource link_id]) Example: 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 17, Mysql_tablename () of all tables in database-gets the format of a database table name: String Mysql_tablename (Resource Result_set, integer index) Example: 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 18, Mysql_fetch_field ()-Get field information format in Result_set returned by Mysql_list_tables () in the specified index index: Object Mysql_fetch_field (Resource reSult [, int field_offset]) Example: 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 < $c ounts; $count + +) {$field = Mysql_fetch_field ($result, $count); echo "<p> $field->name $field->type ($field->max _length) </p> ";} Description: The returned object has a total of 12 object properties: Name: Field Name table: Field Max_length: The maximum length of the field Not_null: If the field cannot be null, then 1, otherwise 0primary_key: 1 if the field is a primary key; Otherwise 0unique_key: If the field is a unique key, then 1, otherwise 0multiple_key: 1 if the field is not unique, otherwise 0numeric: If the field is numeric then 1, otherwise 0blob: if it is a blob then 1, otherwise 0type: The data type of the field unsigned: 1 if the field is unsigned, otherwise 0zerofill: 1 if the field is 0 filled, otherwise 019, Mysql_num_fields ()-Gets the number of fields in the query format: integer mysql_num _fields (Resource Result_set) Example: $query = "Select Id,name from MyTable order by name"; $result = mysql_query ($query); echo "This check The number of fields polled is: ". Mysql_num_fields ($result)." <br/> "; 20, Mysql_list_fields ()-Gets the field name format for all fields of the specified table: Resource Mysql_list_fields (string database_name, string table _nAme [, Resource link_id]) Example: $fields =mysql_list_fields ("MyDatabase", "MyTable"), echo "Number of fields in table MyDatabase in database MyTable:". Mysql_num_fields ($fields). " <br/> "; 21, Mysql_field_flags ()-Gets the specified field option format: String mysql_field_flags (Resource Result_set, Integer field_offset Example: $query = "SELECT ID, name from MyTable order by name"; $result = mysql_query ($query); $row =mysql_fetch_wor ($row); 22, MYSQ L_field_len ()-Gets the maximum length format for the specified field: Integer mysql_field_len (Resource result_set, integer field_offset) Example: $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,21523, Mysql_field_name ()-Gets the field name format: String mysql_field_name (Resource result_set, int field_offset) Example: $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:PKID24, Mysql_fieLd_type ()-Gets the field type format: String Mysql_field_type (Resource result_set, int field_offset) Example: $query = "SELECT ID, name from MYTABL E order by name "; $result = mysql_query ($query); $row = Mysql_fetch_row ($result); Echo mysql_field_type ($result, 0); Result:int25, mysql_field_table ()-Gets the table name format in which the field is located: String mysql_field_table (Resource result_set, int Field_offset) Example: $ 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
Change to: http://www.jb51.net/article/24799.htm

MySQL common functions in PHP (PHP operating database prerequisites)

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.