Analyze and introduce common PHP database solutions. We will encounter many problems when using PHP to connect to the database. This article exposes common database problems in PHP applications, including database pattern design and database access.
PHPYou may encounter many problems when connecting to the database, this article exposes common database problems in PHP applications-including database pattern design, database access and use of database business logic code-and their solutions. If there is only one way to use the database, it is correct.
You can create PHP database design, database access, and database-based PHP business logic code in many ways, but it generally ends with errors. This article describes the five common problems in the PHP code for database design and database access, and how to fix these problems.
PHP database Question 1: use MySQL directly
A common problem is that older PHP code directly uses the mysql _ function to access the database. Listing 1 shows how to directly access the database.
Listing 1. Access/get. php
- <?php
- function get_user_id( $name )
- {
- $db = mysql_connect( 'localhost', 'root', 'password' );
- mysql_select_db( 'users' );
- $res = mysql_query( "SELECT id FROM users WHERE login='".$name."'" );
- while( $row = mysql_fetch_array( $res ) ) { $id = $row[0]; }
- return $id;
- }
- var_dump( get_user_id( 'jack' ) );
- ?>
Note that the mysql_connect function is used to access the database. Pay attention to the query. use string connection to add the $ name parameter to the query. This technology has two good alternatives: the pear db module and the PHP Data Objects (PDO) class. Both provide abstraction from the choice of a specific database. Therefore, your code can be stored in IBM without too many adjustments? DB2? , MySQL, PostgreSQL, or any other database you want to connect. Another value of using the pear db module and the PDO abstraction layer is that you can use it in SQL statements? Operator. This makes SQL easier to maintain and protects your applications from SQL injection attacks.
Listing 2. Access/get_good.php
- <?php
- require_once("DB.php");
- function get_user_id( $name )
- {
- $dsn = 'mysql://root:password@localhost/users';
- $db =& DB::Connect( $dsn, array() );
- if (PEAR::isError($db)) { die($db->getMessage()); }
- $res = $db->query( 'SELECT id FROM users WHERE login=?',array( $name ) );
- $id = null;
- while( $res->fetchInto( $row ) ) { $id = $row[0]; }
- return $id;
- }
- var_dump( get_user_id( 'jack' ) );
- ?>
Note that all the locations that directly use MySQL are eliminated, except for the database connection strings in $ dsn. In addition, we pass? The operator uses the $ name variable in SQL. Then, the queried data is sent in through the array at the end of the query () method.
PHP database Question 2: Do not use the auto increment function
Like most modern databases, MySQL can create automatic incremental unique identifiers based on each record. In addition, we will still see the code, that is, first run a SELECT statement to find the largest id, then add this id to 1, and find a new record. Listing 3 shows an example of bad mode.
Listing 3. Badid. SQL
- DROP TABLE IF EXISTS users;
- CREATE TABLE users (
- id MEDIUMINT,
- login TEXT,
- password TEXT
- );
- INSERT INTO users VALUES ( 1, 'jack', 'pass' );
- INSERT INTO users VALUES ( 2, 'joan', 'pass' );
- INSERT INTO users VALUES ( 1, 'jane', 'pass' );
Here, the id field is simply specified as an integer. Therefore, although it should be unique, we can add any value, as shown in the INSERT statements following the CREATE statement. Listing 4 shows the PHP code that adds users to this type of mode.
Listing 4. Add_user.php
The code in add_user.php first executes a query to find the maximum value of the id. Then, the file adds 1 to the id value to run an INSERT statement. This code fails in the race condition on a server with heavy loads. In addition, it is also inefficient. So what is the alternative? Use the automatic incremental feature in MySQL to automatically create a unique ID for each insert.
- <?php
- require_once("DB.php");
- function add_user( $name, $pass )
- {
- $rows = array();
- $dsn = 'mysql://root:password@localhost/bad_badid';
- $db =& DB::Connect( $dsn, array() );
- if (PEAR::isError($db)) { die($db->getMessage()); }
- $res = $db->query( "SELECT max(id) FROM users" );
- $id = null;
- while( $res->fetchInto( $row ) ) { $id = $row[0]; }
- $id += 1;
- $sth = $db->prepare( "INSERT INTO users VALUES(?,?,?)" );
- $db->execute( $sth, array( $id, $name, $pass ) );
- return $id;
- }
- $id = add_user( 'jerry', 'pass' );
- var_dump( $id );
- ?>
We hope that this article will give you a better understanding of the PHP database solution.
PHP will encounter many problems when connecting to the database. This article exposes common database problems in PHP applications, including database pattern design and database access...