Source: www.freeelamp.com
Lamp architecture website, I used to pay more attention to installation / configuration, and the relative development is relatively small, because I have fewed development. The original text of this article is from:
Published on the o'reilly network (http://www.oreillynet.com/)
http://www.oreillynet.com/pub/a/onlamp/2002/04/04/webdb.html
After reading, it is quite inspired. Some problems encountered in the previous development, the blades are solved. So translate out and share it.
1. Use of PHP
When operating the database, use the associative array (Associative-indexed arrays) is very helpful. Here we look at an array traversal of a basic digital format:
PHP
$ TEMP [0] = "richmond";
$ TEMP [1] = "TiGERS";
$ TEMP [2] = "premiers";
For ($ x = 0; $ x { Echo $ TEMP [$ x]; echo "" } ?> However, another way to save the code is: PHP $ TEMP = Array ("Richmond", "TIGERS", "premiers"); Foreach ($ TEMP AS $ ELEMENT) Echo "$ Element"; ?> Foreach can also output text subscripts: PHP $ TEMP = Array ("Club" => "richmond", "nickname" => "tigers", "AIM" => "premier"); Foreach ($ TEMP AS $ Key => $ VALUE) Echo "$ key: $ value"; ?> Approximately 50 functions for processing arrays are described in the PHP manual. 2. Add variables in the PHP string This is very simple: PHP $ TEMP = "Hello" Echo "$ TEMP World"; ?> However, it should be noted that although there is no error below: PHP $ TEMP = Array ("One" => 1, "Two" => 2); // Output :: The first Element IS 1 Echo "The First Element IS $ TEMP [One]." ?> However, if the Echo statement is not caused by double quotes, it is necessary to report an error. Therefore, it is recommended to use the curne brackets: PHP $ TEMP = Array ("One" => 1, "Two" => 2); Echo "The first element is {$ TEMP [" one "]}." ?> 3. Adopt associative array access query results Look at the example below: PHP $ connection = mysql_connect ("localhost", "albert", "shhh"); mysql_select_db ("WINESTORE", $ Connection); $ result = mysql_query ("Select Cust_ID, Surname, Firstname from Customer, $ Connection; While ($ row = mysql_fetch_array ($ result)) { Echo "ID: / t {$ row [" Cust_ID "]} / n"; Echo "Surname / T {$ ROW [" Surname "]} / n"; Echo "First Name: / t {$ ROW [" firstname "]} / n / n"; } ?> Function mysql_fetch_array () puts a line of query results into an array, can be referenced in two ways, such as CUST_ID, can be used in both ways: $ ROW ["Cust_ID"] or $ ROW [0]. Obviously, the former readability is much better than the latter. In the multi-table chart, if the two column names are the same, it is best to separate them: Select Winery.name as WNAME, Region.name as RNAME, From Winery, Region WHERE WINERY.REGON_ID = region.region_id; The column name is: $ ROW ["WNAME"] and $ ROW ["RNAME"]. In the case of specifying the table name and column name, only the column name: Select Winery.Region_ID From Winery The column name is: $ ROW ["Region_ID"]. The reference to the aggregation function is the reference name: SELECT Count (*) From customer; The column name is: $ ROW ["count (*)"]. 4. Pay attention to common PHP bugs Common PHP error correction is: No page rendered by the web browser when more is expected A pop-up dialog stating what the "Document Contains No Data" A Partial Page When More Is Exected Most of these situations are not the logic of scripts, but the bugs of bugs present in HTML, the HTML BUG generated by the script. For example, a closed tag similar to table>, form>, frame> is missing, and the page cannot be refreshed. The way to solve this problem is to view the source code of HTML. For complex, you cannot find out the page, you can analyze it through the W3C page checkpoint http://validator.w3.org/. If there is no variable, or the variable definition error will make the program be quirky. For example, the following dead cycle: PHP For ($ counter = 0; $ counter <10; $ counter ) Myfunction (); ?> Variable $ Counter is increasing, and $ counter is less than 10. Such errors are generally found by setting higher error reporting levels: Php Error_Reporting (E_ALL); For ($ counter = 0; $ counter <10; $ counter ) Myfunction (); ?> 5. Use the header () function to handle single part query In many web database applications, some features often let users click on a connection, continue to stay in the current page, such a job I call it "single part query". Below is a script called calling.php:
"- // W3C // DTD HTML 4.0 Transitional // En" "http://www.w3.org/tr/html4/loose.dtd">
hEAD>
body>
html>
When the user clicks on the above connection, you will call an action.php. Below is the source code of Action.php:
PHP
// Database function
/ / Redirection
Header ("Location: $ http_referer");
EXIT;
?>
There are two common errors here to remind:
When you call the header () function, you want to include an exit statement to stop the script, otherwise the subsequent script may output before the header is sent.
Header () Function is a common error:
Warning: Cannot Add Header Information - Headers Already SENT ...
The header () function can only be called before the HTML output, so you need to check the blank, space, and the like that may exist in front of PHP.
6. Reload problem and its resolution
When I used the PHP program, I often touched the page refresh, and the database was more handled more.
Let's look at AddCust.php:
PHP
$ query = "Insert Into Customer
SET SURNAME = $ Surname,
Firstname = $ firstname ";
$ connect = mysql_connect ("localhost", "fred", "shhh");
MySQL_SELECT_DB ("WINESTORE", $ Connection;
$ result = mysql_query ($ query, $ connection);
?>
"- // W3C // DTD HTML 4.0 Transitional // En"
"http://www.w3.org/tr/html4/loose.dtd">
hEAD>
I'VE INSERTED The Customer for you.
body>
html>?>
Suppose we use this program with the following connection:
http://www.freeelamp.com/addcust.php?srainame=smith&firstname=fred
If this request is only submitted once, OK, there will be no problem, but if you are refreshed multiple times, you will have multiple records.
This problem can be resolved through the header () function: Below is the new version of AddCust.php:
PHP
$ query = "Insert Into Customer
SET SURNAME = $ Surname,
Firstname = $ firstname ";
$ connect = mysql_connect ("localhost", "fred", "shhh");
MySQL_SELECT_DB ("WINESTORE", $ Connection;
$ result = mysql_query ($ query, $ connection);
Header ("Location: Cust_Receipt.php");
?>
This script redirects the browser to a new page: CUST_RECEIPT.PHP:
"- // W3C // DTD HTML 4.0 Transitional // En"
"http://www.w3.org/tr/html4/loose.dtd">
hEAD>
I'VE INSERTED The Customer for you.
body>
html>
In this way, there is no side effects that the original page continues to refresh.
7. Use lock mechanism to improve application performance
If we have to run a report urgently, we can write a lock to the table, prevent others reading and writing, to improve the processing speed of this table.
8. Develop fast scripts with mysql_unbuffered_query ()
This function can be used to replace the mysql_query () function, the main difference is that mysql_unbuffered_query () is returned immediately after the query, does not need to wait or lock the database.
However, the number of rows returned cannot be checked with a mysql_num_rows () function, because the result set size is unknown.