8 tips for PHP and MySQL development _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags web database
8 tips for PHP and MySQL development. 1. when using arrays in PHP to operate databases, it is very helpful to use an associated array (associatively-indexedarrays). let's look at an array traversal in the basic numeric format: 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:

The code is as follows:


$ Temp [0] = "richmond ";
$ Temp [1] = "tigers ";
$ Temp [2] = "premiers ";

For ($ x = 0; $ x {
Echo $ temp [$ x];
Echo "";
}
?>


However, another way to save code is:

The code is as follows:


$ Temp = array ("richmond", "tigers", "premiers ");
Foreach ($ temp as $ element)
Echo "$ element ";
?>


Foreach can also output text subscript:

The code is as follows:


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

The code is as follows:


$ Temp = "hello"
Echo "$ temp world ";
?>


However, it should be noted that, although the following example is not incorrect:

The code is as follows:


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

The code is as follows:


$ Temp = array ("one" => 1, "two" => 2 );
Echo "The first element is {$ temp [" one "]}.";
?>


3. access query results using correlated arrays
See the following example:

The code is as follows:


$ 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: \ 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 code is as follows:


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:

The code is as follows:


Error_reporting (E_ALL );

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


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:

The code is as follows:





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:

The code is as follows:


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

The code 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 );
?>
"-// W3C // dtd html 4.0 Transitional // EN"
Http://www.w3.org/TR/html4/loose.dtd>


Customer insert


I 've inserted the customer for you.


?>


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:

The code 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 );
He

Header ("Location: cust_receipt.php ");
?>


This script redirects the browser to a new page: cust_receipt.php:

The code is as follows:





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 use of arrays in http://www.bkjia.com/PHPjc/322576.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322576.htmlTechArticle1. PHP when operating the database, the use of the associated array (associatively-indexed arrays) is very helpful, we look at a basic number format array traversal :...

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.