Expose the five common database titles that PHP uses to present in a program-Business logic code that includes database pattern design, database visits, and application databases-and their solutions. If there is only one way for the application database to accurately expose the five common database titles presented by the PHP application, including the database pattern design, database access, and the business logic code of the application database-and their solution plan.
If there is only one method for applying the database to be accurate ......
You can use many methods to create database design, database visits, and database-based PHP business logic code, but it generally ends with errors. This article illustrates the five common headers presented in the PHP code for database design and database access, and how to fix these headers.
Title 1: directly apply MySQL
A common title is that older PHP code directly uses the mysql _ function to visit the database. Listing 1 shows how to directly visit 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 '));
?> |
Pay attention to the application of the mysql_connect function to visit the database. Pay attention to the query, and apply the string connection to add the $ name parameter to the query.
This technique has two good exchange plans: pear db module and PHP Data Objects (PDO) class. Both choose supply abstraction from a specific database. Therefore, your code can be stored in IBM without too much adjustment? 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 apply it in SQL statements? Identifier. In this way, SQL is easier to protect and your application is protected from SQL injection attacks.
The change code for the application pear db is as follows.
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? Apply the $ name variable to the SQL operator. Then, the queried data is sent in through the array at the end of the query () method.
Title 2: no active incremental effects applied
Like most modern databases, MySQL can create active 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, 'job', 'pass ');
Insert into users VALUES (1, 'Jane ', 'pass '); |
The id field is 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
<? 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;
$ Something = $ db-> prepare ('Insert INTO users VALUES (?,?,?) ');
$ Db-> execute ($ TH, array ($ id, $ name, $ pass ));
Return $ id;
}
$ Id = add_user ('Jerry ', 'pass ');
Var_dump ($ id );
?> |
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 change plan? Apply the active incremental feature in MySQL to create a unique ID for each insert. The updated mode is as follows.
Listing 5. Goodid. php
Drop table if exists users;
Create table users (
Id mediumint not null AUTO_INCREMENT,
Login text not null,
Password text not null,
Primary key (id)
);
Insert into users VALUES (null, 'Jack', 'pass ');
Insert into users VALUES (null, 'job', 'pass ');
Insert into users VALUES (null, 'Jane ', 'pass '); |
We have added the not null flag to trick the field from being NULL. We also added the AUTO_INCREMENT tag to enable the field to be automatically incremental, and added the primary key tag to trick that field into being an id. These changes speed up. Listing 6 shows the updated PHP code, which inserts users into the table.
Listing 6. Add_user_good.php
<? Php
Require_once ('Db. php ');
Function add_user ($ name, $ pass)
{
$ Dsn = 'MySQL: // root: password @ localhost/good_genid ';
$ Db = & DB: Connect ($ dsn, array ());
If (PEAR: isError ($ db) {die ($ db-> getMessage ());}
$ Something = $ db-> prepare ('Insert INTO users VALUES (null ,?,?) ');
$ Db-> execute ($ TH, array ($ name, $ pass ));
$ Res = $ db-> query ('select last_insert_id ()');
$ Id = null;
While ($ res-> fetchInto ($ row) {$ id = $ row [0];}
Return $ id;
}
$ Id = add_user ('Jerry ', 'pass ');
Var_dump ($ id );
?> |
Now, instead of getting the largest id value, I directly apply the INSERT statement to INSERT data, and then apply the SELECT statement to retrieve the id of the last inserted record. This code is much simpler and more efficient than the original version and its coherent mode.
[1] [2] [3] [4] Next page