Summary of problems encountered in PHP + MYSQL website development (1) _ MySQL

Source: Internet
Author: User
Summary of problems encountered during PHP + MYSQL website development (1) for websites with LAMP architecture, I used to focus mostly on installation/configuration, but rarely on development, because I am engaged in less 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 that follows 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.";?>

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); while ($ row = mysql_fetch_array ($ result) {echo" ID: tn "; echo" Surnametn "; echo" First name: tnn ";}?>

The mysql_fetch_array () function puts a row of the query result into an array and can be referenced in two ways at the same time. for example, cust_id can be referenced in the following two ways at the same time: $ row ["cust_id"] or $ row [0]. Obviously, the former is much more readable than the latter.

In multi-table join queries, if the names of the two columns are the same, it is best to separate them with aliases:

SELECT winery. name AS wname, region. name AS rname, FROM winery, region WHERE winery. region_id = region. region_id;

The column names are referenced as $ row ["wname"] and $ row ["rname"].

When the table name and column name are specified, only the column name is referenced:

SELECT winery. region_id
FROM winery

Column name reference: $ row ["region_id"].

The reference of the aggregate function is the reference name:

SELECT count (*)
FROM customer;

Column name reference: $ row ["count (*)"].

4. pay attention to common PHP bugs

Common PHP error correction problems are:


No page rendered by the Web browser when much more is expected A pop-up dialog stating that the "Document Contains No Data" A partial page when more is expected

Most of the reasons for these problems are not the logic of the script, but the bug in HTML or the HTML bug generated by the script. For example
, And so on, the page cannot be refreshed. To solve this problem, check the source code of HTML.

For complex pages that cannot find the cause, you can analyze it through W3C page validation program http://validator.w3.org.

If no variable is defined, or the definition of the variable is incorrect, the program will become odd. For example, the following endless loop:

For ($ counter = 0; $ counter <10; $ Counter ++) myFunction ();?>

The variable $ Counter is increasing, while the variable $ counter is always less than 10. Errors of this type can be found by setting a high error report level:

Error_reporting (E_ALL); for ($ counter = 0; $ counter <10; $ Counter ++) myFunction ();?>

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.