Summary of problems encountered during PHP + MYSQL website development (2) 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>
Click here!
When you click the connection above, you can call action. php. The following is the source code of action. php:
// Database functions
// Redirect
Header ("Location: $ HTTP_REFERER ");
Exit;
?>
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.
A common error in the header () function is:
Warning: Cannot add header information-headers already 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:
$ 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);?> "-// W3C // dtd html 4.0 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> I 've inserted the customer for you.?>
Suppose we use the following connection to use this program:
Http://www.freelamp.com/addcust.php? Surname = Smith & 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>
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.