The database operation method function commonly used in PHP development

Source: Internet
Author: User
Tags comments numeric mysql query mysql tutorial php script php tutorial

Today in the PHP tutorial development commonly used in the database tutorial operation method Function Summary analysis, hope that interested friends can refer to.
First, the database operation
1. Connect MySQL Data
MySQL Tutorial _connect ()

Tips and comments
Note: As soon as the script is finished, the connection to the server is closed unless the mysql_close () has been explicitly invoked before.
Tip: To create a persistent connection, use the Mysql_pconnect () function.
Example

<?php
$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.111cn.net Unable to connect, please check C Onnection paremeters ');


2. Select Database
mysql_select_db ()
After connecting to the database, the PHP default selection of the database is not necessarily the database we need to do later, in order to ensure that the database selection is correct, generally after the database connection statement plus the database selection 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. Specify the database to select.
Connection optional. Specify the 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 statement
mysql_query ()
The function sends the SQL statement to the currently active database and executes the statement, returning the result.

Definitions and usage
The mysql_query () function executes a MySQL query.
Grammar
mysql_query (query,connection)
Parameter description
Query required. Specify the SQL query to send. Note: The query string should not end with a semicolon.
Connection optional. Specify 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 the Select,show,explain or DESCRIBE statement and FALSE if the query executes incorrectly.
For other types of SQL statements, mysql_query () returns TRUE on successful execution, and returns FALSE when an error occurs.
A return value other than FALSE means that the query is legitimate and can be executed by the server. This does not indicate any number of rows that are affected or returned. It is quite possible that a query execution succeeded 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 turn off databases that do not need to continue to be active, but this method is not required, and generally PHP automatically shuts down databases that are not active.
e.g.
Mysql_close ($DB);
5. Release SQL results
Mysql_free_result ()
This function is used to free memory that is consumed by the results of the mysql_query (), which is rarely invoked unless the result is large, consumes too much memory, and typically automatically frees up memory after the execution of the PHP script.
e.g
The Ysql_free_result () function frees the resulting memory.
If successful, returns True, or False if it fails.
Grammar
Mysql_free_result (data)
Parameter description
Data required. The result identifier to be freed. The result identifier is the result returned from mysql_query ().
Tips and comments
Note: mysql_free_result () only needs to be invoked when considering the amount of memory that will be consumed when a large result set is returned. All associated memory is automatically freed after the end of the script.
Example

<?php
$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));

Free memory

Mysql_free_result ($result);

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

Mysql_close ($con);
?>

Second, the SQL performs the result operation
1. Returns a row in the execution result
Mysql_fetch_row ()
Returns an array of values for the current row of the execution result, which, after executing this function, points to the next line.
e.g.
$row = Mysql_fetch_row ($result);
Processing execution results are typically placed in the while loop, traversing each row
e.g.
while ($row = Mysql_fetch_row ($result))
{......}
2. Alternative method of Mysql_fetch_row ()
Mysql_fetch_array ()
MYSQL_FETCH_ASSOC ()
Mysql_fetch_array () returns the array of key values, and the key is the column name of the table on which the query is queried;
MYSQL_FETCH_ASSOC () can be sorted (if an optional parameter is assigned) when the result is returned, equivalent to Mysql_fetch_array () +mysql_assoc
3. Field (column) property of the execution result
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 get information about the fields from the query results. If no field offset is specified, the next field that has not been fetched by Mysql_fetch_field () is extracted.
This function returns an object that contains field information.
The properties of the returned object are:
Name-Column Name
Table-the list name of the column
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 the 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 the mysql_query ().
Field_offset required. Specify which field to start from. 0 indicates the first field. If not set, the next field is retrieved.
Tips and comments
Note: The field names returned by this function are case-sensitive.
Example

<?php
$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. "<br/>";
echo "Table name:". $property->table. "<br/>";
echo "Default value:". $property->def. "<br/>";
echo "Max length:". $property->max_length. "<br/>";
echo "Not NULL:". $property->not_null. "<br/>";
echo "Primary Key:". $property->primary_key. "<br/>";
echo "Unique Key:". $property->unique_key. "<br/>";
echo "mutliple Key:". $property->multiple_key. "<br/>";
echo "Numeric Field:". $property->numeric. "<br/>";
echo "BLOB:". $property->blob. "<br/>";
echo "Field Type:". $property->type. "<br/>";
echo "Unsigned:". $property->unsigned. "<br/>";
echo "zero-filled:". $property->zerofill. "<br/><br/>";
}

Mysql_close ($con);
?>

4. Querying the table name 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);
The Echo database contains the following table: ";
while ($row = Mysql_fetch_row ($result))
{
echo $row [0];
}


5. Query the database column name (field name)

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 obtain the number of rows affected by the Insert,update or DELETE query, use Mysql_affected_rows ().
Returns the number of rows that performed the result.
e.g.

$num = mysql_num_rows ($result);


2. Mysql_num_fields ()
Returns the number of columns (fields) in which the result was executed.
e.g. $num = Mysql_num_fields ($result);
3.mysql_set_charset ()
Sets the encoding of the execution result to prevent garbled text when displayed in the 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 is a predefined content, such as define (Mysql_host, ' localhost ');

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.