PHP link mysql common extension function, PHP link mysql_php tutorial

Source: Internet
Author: User
Tags field table mysql functions

PHP link mysql common extension function, PHP link mysql

First, PHP connection database and basic operations

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 then return the results to the user.

In PHP, SQL is divided into two categories (see SQL Statement Classification): First, there are DQL statements that return the result set, such as the Select/desc table name, which requires PHP to process the result set after execution, and two that have no result set, such as DML, DDL, etc. However, the execution of the DML statement has an impact on the record of the data table.

Connection database, common parameters are 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, and the failed return False,mysql_error displays an error message

Select the database, Mysql_error () is only used in debugging, and no more when the project is deployed, otherwise it will reveal the database information
mysql_select_db (' test ') or Die (' Select Database Failed '. Mysql_error ());

mysql_query () to 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

Processing the result set, insert belongs to DML and affects the record of the table
if ($result && mysql_affected_rows () > 0)
MYSQL_INSERT_ID () returns the Auto_increment value of the last new record
Echo ' Insert data success '. MYSQL_INSERT_ID (). '
echo ' Insert data failed with error number: '. Mysql_errno (). ' Error message: '. Mysql_error (). '

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 to process individual fields

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

If you need access to the data in the result set, you can use one of the following four functions (both with the result set resource as parameters and automatically return to the next record, returning 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 (): Get a row from the result set as the associated data save

3. Mysql_fetch_array (): Gets a row from the result set as an associative array, or as an array of numbers, or both. The returned data pattern can be specified using MYSQL_ASSOC (associative array form), Mysql_num (indexed array form), and Mysql_both as the second parameter.

4. Mysql_fetch_object (): Gets a row from the result set as an object, and each field is accessed as an object.

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 the result set

5, Mysql_data_seek (int $num): Moves the pointer of the internal 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 $row[,mixed $field] ): Returns the contents of a cell in the MySQL result set. The field parameter can be either the offset of the field or the field name, or the Field table Point field name (Tablename.fieldname). If the column is aliased (' select Foo as bar from ... '), the column name is replaced with an alias. Calling mysql_result () cannot be mixed with other functions that process the result set.

PHP's application for connecting MySQL functions

The Mysql_fetch_array () function takes a row from the result set as an associative array, or as a numeric array, or both.
Returns an array based on the rows obtained from the result set, or False if there are no more rows.

Mysql_fetch_array (Data,array_type)
Parameter data: Optional. Specifies the data pointers to be used. The data pointer is the result of the mysql_query () function.
Parameters: Array_type Optional. Specifies what kind of results to return. Optional value for this parameter: MYSQL_ASSOC-associative array
Mysql_num-numeric array
Mysql_both-Default. Both associative and numeric arrays are produced.
Note: mysql_fetch_array () is an extended version of Mysql_fetch_row (). In addition to storing data as a numeric index in an array, you can store the data as an associated index, using the field name as the key name.

$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 the person WHERE lastname= ' Adams '";
$result = mysql_query ($sql, $con);
Print_r (Mysql_fetch_array ($result));

Mysql_close ($con);
The output is similar to:
[0] = = Adams
[LastName] = Adams
[1] = John
[FirstName] = John
[2] = London
[City] = London
The MYSQL_FETCH_ASSOC () function obtains a row from the result set as an associative array.
Returns an associative array based on the rows obtained from the result set, or False if there are no more rows.
Parameter: Data (required) A pointer to be used. The data pointer is the result returned from mysql_query ().

Note: Mysql_fetch_assoc () and mysql_fetch_array () plus the second optional parameter Mysql_assoc exactly the same. It simply returns an associative array. This is also the initial working mode of mysql_fetch_array ().
Tip: If you need a numeric index outside of the associated index, use Mysql_fetch_array ().
Note: The field names returned by this function are case-sensitive.

Examples are as follows:
$con = mysql_connect ("localhost", "Hello", "321");
if (! $con)
Die (' Could not connect: '. Mysq ... Remaining full text >>

For PHP connection MySQL function

1. notice:undefined variable:db in C:\xampp\htdocs\shop\files\mysql.php on line 5
Warning: Undefined variable db (line 5th is not quite clear which lines of code).

This error indicates that, from the known code, the reason is that you refer to a variable (db) defined in the body of a function in the body of the function, and from the code it is not noticed that the problem applies to the scope range (global, local) error of the variable. The

simply says that there is no DB found in the function select_mycx.


(1). Pass in the parameter.

Function Select_mycx ($table, $by, $select _str, $number, $db)

(2). Define a global variable reference in the parameter body:

Function Select_mycx ($table, $by, $select _str, $number)
Global $db;


2.Fatal error:call to a member function query () on a non-object in C:\xampp\htdocs\shop\files\mysql.php on L INE 5

This error is actually caused by the error above, because $DB is not introduced correctly, so the query is not executed correctly. true techarticle PHP link mysql common extension functions, PHP link mysql one, PHP connection database and basic operation MySQL uses the ' client/server ' architecture. Use PHP to install the MySQL extension function, and straight ...

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