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

Source: Internet
Author: User
Tags web database
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"

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

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" ""> I 've inserted the customer for you.?>

Suppose we use the following connection to use this program:

Http:// 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"

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.

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: 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.