Common extension functions of PHP linking MySQL

  • 2021-07-22 09:17:57
  • OfStack

1. PHP connection database and basic operation

MySQL uses a'client/server 'architecture. PHP installed MySQL extension function, and directly use the client software area to access MySQL database server, principle 1, all need to send SQL command to MySQL management system, and then return the result to the user.

In PHP, SQL is divided into two categories (see SQL statement classification): 1 is an DQL statement that returns a result set, such as select/desc table name. After execution, PHP is required to process the result set; 2 has no result set, such as DML, DDL, etc., but the successful execution of DML statement has an impact on the records of the data table.


<?php
// Connect to a database , Common parameters are host name, user name, and password
$link = mysql_connect('localhost','root','123456');
// Determine whether the connection was successful
if(!$link)
{
die(' Connection failed '.mysql.error()); // Successful connection returns resource identifier, failed connection returns false , mysql_error Display error message
}

// Select a database ,mysql_error() Use it only for debugging, and don't use it when deploying the project, otherwise it will reveal database information
mysql_select_db('test') or die(' Failed to select database '.mysql_error());

//mysql_query() You can set character sets and execute SQL Statement
mysql_query('set names utf-8');
$sql = 'insert into test(id,name) values("1","dwqs")';
$result = mysql_query($sql); // Execute sql Returns a result set

// Processing the result set, insert Belong to DML Will have an impact on the records of the table
if($result && mysql_affected_rows() > 0)
{
//mysql_insert_id() Return to the last 1 Of a new record auto_increment Value
echo ' Insert data successfully '.mysql_insert_id().'<br/>';
}
else
{
echo ' Failed to insert data, error number :'.mysql_errno().' Error message: '.mysql_error().'<br/>';
}

// Close the connection
mysql_close($link);
?>

2. PHP processing select query result set

Executing select statement in PHP returns 1 result set, which can be used to process each field


$result = mysql_query('select * from test');
// Gets the number of record rows
$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 take the result set resource character as an argument and automatically return the next 1 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 1 row from the result set and save it as associated data

3. mysql_fetch_array (): Get 1 row from the result set as an associative array, or an array of numbers, or both. You can use MYSQL_ASSOC (associative array form), MYSQL_NUM (indexed array form), and MYSQL_BOTH as second parameters to specify the shape of the returned data.

4. mysql_fetch_object (): Get 1 row from the result set as an object, and each field is accessed as an object.

Recommendation: Do not use mysql_fetch_array () without special requirements, but use mysql_fetch_row () or mysql_fetch_assoc () to achieve the same function efficiently.

There are also three commonly used functions related to result sets

5. mysql_data_seek (int $num): Move the pointer to the internal result, $num is the number of rows of the new result set pointer you want to set.

6. mysql_fetch_lengths (resource) $result ): Gets the length of each output in the result set

7. mysql_result (resource) $result , int $row[,mixed $field] ): Returns the contents of 1 cell 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 a column is aliased ('select foo as bar from...'), the column name is replaced by an alias. Calling mysql_result () cannot be mixed with other functions that process result sets.


Related articles: