Php connection database code Application Analysis

Source: Internet
Author: User
Tags mysql functions
In php, database operations are commonly used. here, code and functions are commonly used in php. The code is as follows:
$ Db_host = 'hostname is database server ';
$ Db_database = 'database name ';
$ Db_username = 'username ';
$ Db_password = 'password ';
$ Connection = mysql_connect ($ db_host, $ db_username, $ db_password); // connect to the database
Mysql_query ("set names 'utf8'"); // Encode and convert
If (! $ Connection ){
Die ("cocould not connect to the database:
". Mysql_error (); // diagnose a connection error
}
$ Db_selecct = mysql_select_db ($ db_database); // select a database
If (! $ Db_selecct)
{
Die ("cocould not to the database
". Mysql_error ());
}
$ Query = "select * from msu"; // Construct a query statement
$ Result = mysql_query ($ query); // execute the query
If (! $ Result)
{
Die ("cocould not to the database
". Mysql_error ());
}
// Array mysql_fetch_row (resource $ result );
While ($ result_row = mysql_fetch_row ($ result) // Retrieve the result and display it
{
$ Num = $ result_row [0];
$ Age = $ result_row [1];
$ Name = $ result_row [2];
Echo"";
Echo"$ Num";
Echo"$ Age";
Echo"$ Name";
Echo"";
}
Mysql_close ($ connection); // close the connection
?>

The code with detailed instructions is as follows:
The code is as follows:
$ Connect = mysql_connect ("127.0.0.1", "root", "") or die ("link error"); // enable link to mysql
$ Select_db = mysql_select_db ("database name", $ connect); // if the connection identifier is not explicitly declared, the last link opened by default
// Execute the SQL statement!
$ SQL = "SELECT * FROM test"
$ Query = mysql_query ($ SQL, $ connect) or die (mysql_error ());

// Difference between the two query functions array/row
$ Row1 = mysql_fetch_row ($ query );
Print_r ($ row1); // only labels in the array can be saved
$ Row2 = mysql_fetch_array ($ query );
Print_r ($ row2); // you can save the labels and field names in the array.
// Loop output while output to an empty position
While ($ row1 ){
Print_r ($ row1 );
}

Mysql_query ("set names 'utf-8 '");

// Calculate the number of query results
Mysql_num_rows ($ query );
// Return the IP address of the last INSERT command
Mysql_insert_id ($ query );
// Obtain the database name
Mysql_tablename ($ query );
// Return error message
Mysql_error ();
// Close the link
Mysql_close ();

PHP connection to mySQL common functions
Mysql can be connected to the web through PHP in two ways, one is Mysql-related functions through php, and the other is ODBC-related functions through php.
The related functions are as follows:
MYSQL functions
Mysql_affected_rows: obtain the number of columns affected by the last MySQL operation.
Mysql_close: disconnect the MySQL server.
Mysql_connect: enables connections to the MySQL server.
Mysql_create_db: create a new MySQL database.
Mysql_data_seek: move internal return indicator.
Mysql_db_query: Send the query string to the MySQL database.
Mysql_drop_db: remove the database.
Mysql_errno: return the error code.
Mysql_error: returns an error message.
Mysql_fetch_array: returns the array data.
Mysql_fetch_field: obtains the field information.
Mysql_fetch_lengths: returns the maximum length of data in each column of a single column.
Mysql_fetch_object: returns object data.
Mysql_fetch_row: returns the columns of a single column.
Mysql_field_name: return the name of the specified field.
Mysql_field_seek: set a field from the indicator to the returned value.
Mysql_field_table: obtain the name of the table in the current column.
Mysql_field_type: obtain the current column type.
Mysql_field_flags: obtains the flag of the current field.
Mysql_field_len: get the length of the current column.
Mysql_free_result: releases the memory occupied by the returned data.
Mysql_insert_id: return the ID of the last INSERT command.
Mysql_list_fields: lists the fields of a specified data table ).
Mysql_list_dbs: list available databases of the MySQL server ).
Mysql_list_tables: lists the tables of a specified database ).
Mysql_num_fields: gets the number of returned fields.
Mysql_num_rows: gets the number of returned columns.
Mysql_pconnect: enables long-term connections to the MySQL server.
Mysql_query: returns a query string.
Mysql_result: Gets the query result.
Mysql_select_db: Select a database.
Mysql_tablename: get the table name.
ODBC functions
To use ODBC functions, you must install MYSQL ODBC
Odbc_autocommit: Enables or disables automatic change.
Odbc_binmode: sets the binary data processing method.
Odbc_close: close the ODBC link.
Odbc_close_all: close all ODBC connections.
Odbc_commit: updates the ODBC database.
Odbc_connect: connect to the ODBC database.
Odbc_cursor: Get the cursor name.
Odbc_do: execute SQL commands.
Odbc_exec: execute SQL commands.
Odbc_execute: execute the preset SQL command.
Odbc_fetch_into: Gets the specified column returned.
Odbc_fetch_row: returns a column.
Odbc_field_name: obtains the column name.
Odbc_field_type: obtains the column data format.
Odbc_field_len: obtains the length of the column data.
Odbc_free_result: refers to the memory used to send data back.
Odbc_longreadlen: sets the maximum value of the return column.
Odbc_num_fields: obtains the number of fields.
Odbc_pconnect: long-term connection to the ODBC database.
Odbc_prepare: preset SQL commands.
Odbc_num_rows: gets the number of returned columns.
Odbc_result: obtain the returned data.
Odbc_result_all: returns HTML table data.
Odbc_rollback: undo the current transaction.
Odbc_setoption: adjusts ODBC settings.

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.