A _php tutorial on database operation method functions commonly used in PHP development

Source: Internet
Author: User
Tags mysql query mysql tutorial
Today in the PHP tutorial development commonly used in the database tutorial operation method Function Summary analysis, I hope that interested friends can refer to.
First, the database operation
1. Connect MySQL Data
MySQL Tutorial _connect ()

Hints and Notes
Note: At the end of the script, the connection to the server is closed, unless Mysql_close () has been explicitly called before.
Tip: To create a persistent connection, use the Mysql_pconnect () function.
Example

$con = mysql_connect ("localhost", "Mysql_user", "mysql_pwd");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}

Some code ...

Mysql_close ($con);
?>

e.g.

$db = mysql_connect (Mysql_host, Mysql_user, Mysql_password) or Die (' www.zhutiai.com unable-connect, please check Connection paremeters ');


2. Select a database
mysql_select_db ()
After connecting to the database, PHP default selection of the database is not necessarily the database we need in the subsequent operations, in order to ensure that the database selection is correct, usually after the database connection statement also add the database SELECT statement.

The mysql_select_db () function sets the active MySQL database.
If successful, the function returns True. If it fails, it returns false.
Grammar
mysql_select_db (database,connection)
Parameter description
Database Required. Specifies the database to select.
Connection is optional. Specify MySQL connection. If not specified, the previous connection is used.

e.g.
mysql_select_db (mysql_db, $db) or Die (Mysql_error ($db));
3. Execute SQL statements
mysql_query ()
The function sends the SQL statement to the currently active database and executes the statement, returning the result.

Definition and usage
The mysql_query () function executes a MySQL query.
Grammar
mysql_query (query,connection)
Parameter description
Query is required. Specifies the SQL query to send. Note: The query string should not end with a semicolon.
Connection is optional. Specifies the SQL connection identifier. If not specified, the previous open connection is used.
Description
If there is no open connection, this function attempts to call the mysql_connect () function without arguments to establish a connection and use it.
return value
mysql_query () returns a resource identifier only for Select,show,explain or DESCRIBE statements, or FALSE if the query is executed incorrectly.
For other types of SQL statements, mysql_query () returns TRUE on successful execution and returns FALSE on error.
A return value other than FALSE means that the query is legitimate and can be executed by the server. This does not indicate any information about the number of rows affected or returned. It is possible that a query executed successfully but did not affect or return any rows.

e.g.

$query = "SELECT * FROM $table"
$result = mysql_query ($query, $db) or Die (Mysql_error ($db));


4. Close the database
Mysql_close ()
This function is used to close a database that does not need to continue to be active, but this method is not required, and generally PHP automatically shuts down databases that do not continue to be active.
e.g.
Mysql_close ($DB);
5. Releasing SQL results
Mysql_free_result ()
This function frees the memory occupied by the mysql_query () execution result, which is seldom called unless the result is very large and takes up too much memory; it usually frees up memory after the execution of the PHP script.
e.g
The Ysql_free_result () function frees the resulting memory.
Returns true if successful, or False if it fails.
Grammar
Mysql_free_result (data)
Parameter description
Data required. The result identifier to release. The result identifier is the result returned from mysql_query ().
Hints and Notes
Note: mysql_free_result () only needs to be called when considering how much memory is consumed when a large result set is returned. All associated memory will be automatically freed after the script ends.
Example

$con = mysql_connect ("localhost", "Peter", "abc123");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}

$db _selected = mysql_select_db ("test_db", $con);

$sql = "SELECT * from person";
$result = mysql_query ($sql, $con);
Print_r (Mysql_fetch_row ($result));

Freeing memory

Mysql_free_result ($result);

$sql = "SELECT * from Customers";
$result = mysql_query ($sql, $con);
Print_r (Mysql_fetch_row ($result));

Mysql_close ($con);
?>

Ii. SQL Execution Result operation
1. Return a row in the execution result
Mysql_fetch_row ()
Returns an array of values for the current row of the execution result, after which the result points to the next line.
e.g.
$row = Mysql_fetch_row ($result);
Processing execution results are typically placed in a while loop, traversing each row
e.g.
while ($row = Mysql_fetch_row ($result))
{......}
2. Alternative methods for Mysql_fetch_row ()
Mysql_fetch_array ()
MYSQL_FETCH_ASSOC ()
Mysql_fetch_array () returns an array of key values, the column name of the table to which the key is queried;
MYSQL_FETCH_ASSOC () The result can be sorted first (if an optional parameter is assigned), which is equivalent to mysql_fetch_array () +mysql_assoc
3. Field (column) Properties for execution results
Mysql_fetch_field ()
eg.
The Mysql_fetch_field () function obtains column information from the result set and returns it as an object.
Mysql_fetch_field () can be used to obtain information about a field from the query results. If no field offset is specified, the next field that has not been Mysql_fetch_field () is fetched.
The function returns an object that contains field information.
The properties of the returned object are:
Name-Column Name
Table-the name of the row in which the column resides
Max_length-Maximum length of the column
NOT_NULL-1, if the column cannot be null
Primary_key-1, if the column is primary key
Unique_key-1, if the column is a unique key
Multiple_key-1, if the column is Non-unique key
Numeric-1, if the column is numeric
Blob-1, if the column is a BLOB
Type-Types of this column
Unsigned-1, if the column is an unsigned number
ZEROFILL-1, if the column is zero-filled
Grammar
Mysql_fetch_field (Data,field_offset)
Parameter description
Data required. The data pointer to use. The data pointer is the result returned from mysql_query ().
Field_offset required. Specifies the field from which to start. 0 indicates the first field. If not set, the next field is retrieved.
Hints and Notes
Note: The field names returned by this function are case-sensitive.
Example

$con = mysql_connect ("localhost", "Hello", "321");
if (! $con)
{
Die (' Could not connect: '. Mysql_error ());
}

$db _selected = mysql_select_db ("test_db", $con);

$sql = "SELECT * from person";
$result = mysql_query ($sql, $con);

while ($property = Mysql_fetch_field ($result))
{
echo "Field name:". $property->name. "
";
echo "Table name:". $property->table. "
";
echo "Default value:". $property->def. "
";
echo "Max length:". $property->max_length. "
";
echo "Not NULL:". $property->not_null. "
";
echo "Primary Key:". $property->primary_key. "
";
echo "Unique Key:". $property->unique_key. "
";
echo "mutliple Key:". $property->multiple_key. "
";
echo "Numeric Field:". $property->numeric. "
";
echo "BLOB:". $property->blob. "
";
echo "Field Type:". $property->type. "
";
echo "Unsigned:". $property->unsigned. "
";
echo "zero-filled:". $property->zerofill. "

";
}

Mysql_close ($con);
?>

4. Querying the table names in the database
We're going to show all the tables using the Mysql_list_tables usage syntax,
Mysql_list_tables syntax

Mysql_list_tables ()
e.g.
$db _name = mysql_db;
$result = Mysql_list_tables ($db _name);
echo "database contains the following table:";
while ($row = Mysql_fetch_row ($result))
{
echo $row [0];
}


5. Querying the Database for column names (field names)

Esource Mysql_list_fields (string database_name, string table_name [, Resource Link_identifier])

Mysql_list_fields () Gets the information for the given table name. The parameter is the database name and the table name. Returns a result pointer that can be used for mysql_field_flags (), Mysql_field_len (), Mysql_field_name (), and Mysql_field_ty

Mysql_list_fields ()
e.g.
$fields = Mysql_list_fields ($db _name, $table);
$columns = Mysql_num_fields ($fields);
for ($i = 0; $i < $columns; $i + +)
Echo Mysql_field_name ($fields, $i);


Third, other functions
1. Mysql_num_rows ()

The mysql_num_rows () function returns the number of rows in the result set.
Grammar
Mysql_num_rows (data)
Parameter description
Data required. The result set. The result set is obtained from the invocation of mysql_query ().
Description
Mysql_num_rows () returns the number of rows in the result set. This command is valid only for SELECT statements. To get the number of rows affected by the Insert,update or DELETE query, use Mysql_affected_rows ().
Returns the number of rows for which the result was executed.
e.g.

$num = mysql_num_rows ($result);


2. Mysql_num_fields ()
Returns the number of columns (number of fields) of the execution result.
e.g. $num = Mysql_num_fields ($result);
3.mysql_set_charset ()
Sets the encoding of the execution result to prevent garbled characters when displaying Chinese in a Web page.
e.g.

$query = "SELECT * from $table _name";
mysql_query (' Set names utf8′);
$result = mysql_query ($query, $db) or Die (Mysql_error ($db));


Note:
1. The uppercase code in the text is predefined content, such as define (Mysql_host, ' localhost ');

http://www.bkjia.com/PHPjc/630744.html www.bkjia.com true http://www.bkjia.com/PHPjc/630744.html techarticle today in the PHP tutorial development commonly used in the database tutorial operation method Function Summary analysis, I hope that interested friends can refer to. One, database operation 1. Connect mysq ...

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