8 tips for PHP and MySQL development

Source: Internet
Author: User

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.