PHP Connection Database Code application Analysis _php Foundation

Source: Internet
Author: User

Copy Code code as follows:

<?php
$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 Database
mysql_query ("Set names ' UTF8 '");//Encoding conversion
if (! $connection) {
Die ("Could not connect to the database:</br>". Mysql_error ())//Troubleshooting connection Error
}
$db _selecct=mysql_select_db ($db _database);//Select Database
if (! $db _selecct)
{
Die ("Could not to the database</br>". Mysql_error ());
}
$query = "SELECT * from MSU";//Build Query statement
$result =mysql_query ($query);//Execute Query
if (! $result)
{
Die ("Could not to the database</br>". Mysql_error ());
}
Array mysql_fetch_row (resource $result);
while ($result _row=mysql_fetch_row ($result))//Remove the result and display
{
$num = $result _row[0];
$age = $result _row[1];
$name = $result _row[2];
echo "<tr>";
echo "<td> $num </td>";
echo "<td> $age </td>";
echo "<td> $name </td>";
echo "</tr>";
}
Mysql_close ($connection);//close connection
?>

Here is the code with detailed instructions
Copy Code code as follows:

$connect = mysql_connect ("127.0.0.1", "Root", "") or Die ("link Error");/open link to MySQL
$select _db = mysql_select_db ("Database name", $connect);//If you do not specifically declare a connection identifier, the last link that was opened is the default
Execute the SQL statement!
$sql = "SELECT * FROM Test"
$query = mysql_query ($sql, $connect) or Die (Mysql_error ());

Array/row difference of two query functions
$row 1 = mysql_fetch_row ($query);
Print_r ($row 1);//Only the labels in the array can be saved
$row 2 = mysql_fetch_array ($query);
Print_r ($row 2);//You can save the label in the array, and the field name
Loop output while output to empty position
while ($row 1) {
Print_r ($row 1);
}

mysql_query ("SET NAMES ' UTF-8 '");

Number used to calculate query results
Mysql_num_rows ($query);
Returns the IP that last used the insert instruction
MYSQL_INSERT_ID ($query);
Get database name
Mysql_tablename ($query);
return error message
Mysql_error ();
Close link
Mysql_close ();

PHP connection MySQL common functions
MySQL can be connected to the Web through PHP in two ways, a MySQL-related function via PHP, and another ODBC-related function through PHP.
The related functions are as follows:
MySQL function
Mysql_affected_rows: Gets the number of columns affected by the MySQL last operation.
Mysql_close: Close the MySQL server connection.
Mysql_connect: Open the MySQL server connection.
mysql_create_db: Create a new MySQL database.
Mysql_data_seek: Move internal return indicator.
Mysql_db_query: Send query string to MySQL database.
mysql_drop_db: Remove the repository.
Mysql_errno: Returns the error message code.
Mysql_error: Send back the error message.
Mysql_fetch_array: Returns the array data.
Mysql_fetch_field: Get field information.
Mysql_fetch_lengths: Returns the maximum length of column data.
Mysql_fetch_object: Returns the object data.
Mysql_fetch_row: Returns a single column of fields.
Mysql_field_name: Returns the name of the specified field.
Mysql_field_seek: Sets the indicator to a field that returns a value.
Mysql_field_table: Gets the table name for the current field.
Mysql_field_type: Gets the current field type.
Mysql_field_flags: Get the flag for the current field.
Mysql_field_len: Gets the current field length.
Mysql_free_result: Release back to occupy memory.
MYSQL_INSERT_ID: Returns the ID for the last time the insert instruction was used.
Mysql_list_fields: Lists the fields (field) for the specified table.
Mysql_list_dbs: Lists the database (database) available for the MySQL server.
Mysql_list_tables: Lists the table (table) for the specified database.
Mysql_num_fields: Gets the number of returned fields.
Mysql_num_rows: Gets the number of returned columns.
Mysql_pconnect: Open the MySQL server long-term connection.
Mysql_query: Send out a query string.
Mysql_result: Gets the results of the query.
mysql_select_db: Select a database.
Mysql_tablename: Gets the table name.
ODBC functions
Use ODBC functions to install MySQL ODBC
Odbc_autocommit: Switch automatic change function.
Odbc_binmode: Set binary data processing mode.
Odbc_close: Close ODBC connection.
Odbc_close_all: Closes all ODBC connections.
Odbc_commit: Changes ODBC database.
Odbc_connect: Link to ODBC repository.
Odbc_cursor: Gets the cursor name.
Odbc_do: Execute SQL directives.
Odbc_exec: Execute SQL directives.
Odbc_execute: Executes preset SQL directives.
Odbc_fetch_into: Gets the specified column returned.
Odbc_fetch_row: Get a column back.
Odbc_field_name: Gets the field name.
Odbc_field_type: Get field data form.
Odbc_field_len: Gets the field data length.
Odbc_free_result: Release the memory that returns the data.
Odbc_longreadlen: Sets the maximum value for the return column.
Odbc_num_fields: Gets the number of fields.
Odbc_pconnect: Link to ODBC database for a long time.
Odbc_prepare: Presets SQL directives.
Odbc_num_rows: Gets the number of returned columns.
Odbc_result: Get the information back.
Odbc_result_all: Returns HTML form data.
Odbc_rollback: Undo current transaction.
Odbc_setoption: adjust 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.