For websites with LAMP architecture, I used to focus mostly on installation/configuration, but there are few development stories, because I am rarely engaged in development. The original text of this article also comes 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, I was inspired to solve some problems encountered in the previous development. Therefore, the translation is shared with everyone.
1. Use of arrays in PHP
It is very helpful to use associatively-indexed arrays when operating the database. Let's take a look at the basic number format of array traversal:
$ Temp [0] = "richmond ";
$ Temp [1] = "tigers ";
$ Temp [2] = "premiers ";
For ($ x = 0; $ x
{
Echo $ temp [$ x];
Echo "";
}
?>
However, another way to save code is:
$ Temp = array ("richmond", "tigers", "premiers ");
Foreach ($ temp as $ element)
Echo "$ element ";
?>
Foreach can also output text subscript:
$ Temp = array ("club" => "richmond ",
"Nickname" => "tigers ",
"Aim" => "premiers ");
Foreach ($ temp as $ key => $ value)
Echo "$ key: $ value ";
?>
The PHP manual describes about 50 functions used to process arrays.
2. Add a variable to the PHP string
This is simple:
$ Temp = "hello"
Echo "$ temp world ";
?>
However, it should be noted that, although the following example is not incorrect:
$ 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 quotation marks, an error is reported. We recommend that you use curly brackets:
$ Temp = array ("one" => 1, "two" => 2 );
Echo "The first element is {$ temp [" one "]}.";
?>
3. Access query results using correlated arrays
See the following example:
$ Connection = mysql_connect ("localhost", "albert", "shhh ");
Mysql_select_db ("winestore", $ connection );
$ Result = mysql_query ("SELECT cust_id, surname,
Firstname FROM customer ", $ connection );