PHP and MySQL developed 8 tips _php Tips

Source: Internet
Author: User
Tags arrays php and mysql php error web database
1. Use of arrays in PHP
Using associative arrays (associatively-indexed arrays) is very helpful when manipulating a database, so let's look at an array traversal of the basic number format:

<?php
$temp [0] = "Richmond";
$temp [1] = "tigers";
$temp [2] = "premiers";

for ($x =0; $x <count ($temp); $x + +)
{
echo $temp [$x];
echo "";
}
?>

Another way to save more code, however, is to:

<?php
$temp = Array ("Richmond", "tigers", "premiers");
foreach ($temp as $element)
echo "$element";
?>

foreach can also output text subscript:

<?php
$temp = Array ("club" => "Richmond",
"nickname" => "Tigers",
"Aim" => "premiers");

foreach ($temp as $key => $value)
echo "$key: $value";
?>
The PHP manual describes about 50 functions that are used to work with arrays.

2. Add a variable to the PHP string

This is very simple:

<?php
$temp = "Hello"
echo "$temp World";
?>

However, it is necessary to note that although the following examples are not incorrect:
<?php
$temp = Array ("One" => 1, "two" => 2);
Output:: The 1
echo "The" is $temp [one]. "
?>

However, if the following Echo statement does not have double quotes, it is an error, so it is recommended that you use curly braces:

<?php
$temp = Array ("One" => 1, "two" => 2);
echo "The" is {$temp ["one"]}. "
?>


3. Using associative array to access query results
Look at the following example:

<?php
$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 "name:\t{$row [" FirstName "]}\n\n";
}
?>

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

In multiple table links, if you have two column names, it's 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"].


In the case of specifying a table name and a column name, only the column name is referenced:

SELECT winery.region_id
From Winery

The reference to the column name is: $row ["region_id"].

A reference to a clustered function is a reference name:

SELECT COUNT (*)
from customer;

The reference to the column name is: $row [Count (*)].

4. Note Common PHP bugs

Common problems with PHP error correction are:

No page rendered by the Web browser as much as expected
A Pop-up dialog stating that "Document Contains No Data"
A Partial page when it is expected

Most of the reasons for this are not in the logic of the script, but in the bugs that exist in the HTML or in the HTML that the script generates. For example, the absence of similar </table&gt, </form&gt, </frame>, such as close Tag, the page can not be refreshed. The solution to this problem is to view the source code of the HTML.

For complex, unable to find the cause of the page, you can through the Web page calibration program http://validator.w3.org/to analyze.

If you do not define a variable, or a variable definition error can make the program odd. For example, the following dead loops:

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

The variable $Counter is increasing and the $counter is always less than 10. This type of error can generally be found by setting a higher error reporting level:

<?php
Error_reporting (E_all);

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

5. Use header () function to process a single part query

In many WEB database applications, some features often allow users to click on a connection, continue to stay on the current page, such a job I call it "single part query."

Here's a script called calling.php:

<! DOCTYPE HTML Public
"-//W3C//DTD HTML 4.0 transitional//en"
"Http://www.w3.org/TR/html4/loose.dtd" >
<title>calling page example</title>
<body>
<a href= "action.php" >click here!</a>
</body>

When the user clicks on the connection above, it calls action.php. The following is the source code of action.php:

<?php
Database Features

redirect
Header ("Location: $HTTP _referer");
Exit
?>

Here are two common bugs that need to be reminded:
Call the header () function to include an exit statement to stop the script, otherwise the subsequent script might output before the header is sent.


One common error with the header () function is:

Warning:cannot Add header information-headers already sent ...

The header () function can only be invoked before the HTML output, so you need to check for possible blank lines, spaces, and so on in front of PHP.

6. The problem of reload and its solution
I used to write PHP programs, often encountered when the page is refreshed, the database processing one more time.
Let's see addcust.php:

<?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);
?>
<! DOCTYPE HTML Public
"-//W3C//DTD HTML 4.0 transitional//en"
"Http://www.w3.org/TR/html4/loose.dtd" >
<title>customer insert</title>
<body>
I ' ve inserted the customer for you.
</body>
?>
Let's say we use this program with the following connection:

Http://www.freelamp.com/addcust. ... &firstname=fred

If this request is submitted only once, OK, there will be no problem, but if you refresh it more than once, you will have more than one record inserted.
This problem can be solved by the header () function: The following is a new version of addcust.php:

<?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);
Header ("Location:cust_receipt.php");
?>
This script redirects the browser to a new page: cust_receipt.php:

<! DOCTYPE HTML Public
"-//W3C//DTD HTML 4.0 transitional//en"
"Http://www.w3.org/TR/html4/loose.dtd" >
<title>customer insert</title>
<body>
I ' ve inserted the customer for you.
</body>
In this way, the original page continues to refresh without side effects.

7. Use lock mechanism to improve application performance
If we want to run a report urgently, then we can write a lock on the table, prevent others read and write, to improve the processing speed of this table.

8. Develop fast scripts with mysql_unbuffered_query ()
This function can be used to replace the mysql_query () function, the main difference is that Mysql_unbuffered_query () after the execution of the query immediately return, do not need to wait or lock the database.

However, the number of rows returned cannot be checked with the mysql_num_rows () function because the result set size of the output is unknown.
Related Article

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.