8 tips for PHP and MySQL development 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:
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 {$ temp [" one "]}.";
?>
3. access query results using correlated arrays
See the following example:
$ 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 ";
}
?>
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,,If tags are disabled, 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:
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:
5. use the header () function to process single part queries
In many Web database applications, some features often allow users to click a connection and stay on the current page. I call this "single part Query ".
The following is a script called calling. php:
"-// W3C // dtd html 4.0 Transitional // EN"
Http://www.w3.org/TR/html4/loose.dtd>
Calling page example
Click here!
When you click the connection above, you can call action. php. The following is the source code of action. php:
There are two common errors to be reminded:
After the header () function is called, an exit statement must be included to stop the script. otherwise, subsequent scripts may be output before the header is sent.
The header () function can only be called before HTML output. Therefore, you need to check possible empty lines and spaces before php.
6. reload Problems and Solutions
When I used to write a PHP program, I often encountered a situation where the database was processed once more when the page was refreshed.
Let's take a look at addcust. php:
?>
Suppose we use the following connection to use this program:
Http://www.freelamp.com/addcust... & firstname = Fred
If this request is submitted only once, OK will not be a problem, but if you refresh multiple times, you will have multiple records inserted.
This problem can be solved through the header () function: the new version of addcust. php is as follows:
$ Query = "insert into customer
SET surname = $ surname,
Firstname = $ firstname ";
$ Connection = 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>
Customer insert
I 've inserted the customer for you.
In this way, the original page continues to refresh without any side effects.
7. use locks to improve application performance
If we want to run a report urgently, we can apply a write lock to the table to prevent reading/writing by others to improve the processing speed of the table.
8. use mysql_unbuffered_query () to develop a quick script.
This function can be used to replace the mysql_query () function. The main difference is that mysql_unbuffered_query () returns immediately after the query is executed, without waiting or locking the database.
However, the number of returned rows cannot be checked using the mysql_num_rows () function, because the size of the output result set is small and unknown.
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.