PHP link mysql common extension function _php instance

Source: Internet
Author: User
Tags field table

PHP Connection database and basic operation

MySQL uses a ' client/server ' architecture. Using the MySQL extension function installed in PHP and accessing the MySQL database server directly using the client software area, the principle is to send SQL commands to the MySQL management system and return the results to the user.

In PHP, SQL is divided into two categories (view SQL statement classification): First, there are DQL statements that return the result set, such as the Select/desc table name, after execution, you need PHP to process the result set, and there is no result set, such as DML, DDL, etc. However, when a DML statement succeeds, it affects the record of the datasheet.

<?php
Connection database, common parameter is host name, user name and password
$link = mysql_connect (' localhost ', ' root ', ' 123456 ');
Determine if the connection is successful
if (! $link)
{
Die (' Connection failed '. Mysql.error ()); The connection successfully returns the resource identifier, failure returns false,mysql_error display error message
}

Select the database, Mysql_error () is only used in debugging, then do not deploy the project, or you will disclose the database information
mysql_select_db (' test ') or Die (' Select Database Failed '. Mysql_error ());

mysql_query () can set character sets and Execute SQL statements
mysql_query (' Set names Utf-8 ');
$sql = ' INSERT INTO Test (Id,name) VALUES ("1", "Dwqs") ";
$result = mysql_query ($sql); Execute SQL return result set

Process result set, insert is DML, affect table record
if ($result && mysql_affected_rows () > 0)
{
MYSQL_INSERT_ID () returns the Auto_increment value of the last new record
echo ' Insert data successfully '. MYSQL_INSERT_ID (). ' <br/> ';
}
Else
{
echo ' Insert data failed, error number: '. Mysql_errno (). ' Error message: '. Mysql_error (). ' <br/> ';
}

Close connection
Mysql_close ($link);
?>

Second, PHP processing select query result set

Executing a SELECT statement in PHP Returns a result set that can be used for processing individual fields

$result = mysql_query (' SELECT * from Test ');
Get the number of rows in a record
$rows = mysql_num_rows ($result);
Gets the number of fields, that is, data columns
$cols = Mysql_num_fields ($result);

If you need to access the data in the result set, you can use one of the following four functions (all with the result set resource character as an argument and automatically return the next record, false at the end of the table)

1, Mysql_fetch_row (): This function returns a result record and saves it as a normal index data

2, Mysql_fetch_assoc (): From the result set to obtain a row as the associated data to save

3, Mysql_fetch_array (): Take one row from the result set as an associative array, or an array of numbers, or both. You can specify the returned data shape using MYSQL_ASSOC (associative array form), Mysql_num (indexed array form), and Mysql_both as the second parameter.

4, Mysql_fetch_object (): From the result set to get a row as an object, the fields are accessed as objects.

Recommendation: No special requirements, do not use mysql_fetch_array (), you can use Mysql_fetch_row () or MYSQL_FETCH_ASSOC () to achieve the same function, and high efficiency.

There are also three common functions associated with result sets

5, Mysql_data_seek (int $num): A pointer to move the inner result, $num is the number of rows of the new result set pointer that you want to set.

6, Mysql_fetch_lengths (Resource $result ): Get the length of each output in the result set

7, Mysql_result (Resource $result , int ): Returns the contents of a unit in the MySQL result set. The field parameter can be the offset of the field or the field name, or the Field table Point field name (Tablename.fieldname). If you alias the column (' Select Foo as bar from ... '), replace the column name with an alias. Calling mysql_result () cannot be mixed with other functions that handle result sets.

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.