PHP and MySQL 8 tips for developing _php tutorials

Source: Internet
Author: User
Tags php and mysql php error web database
Source: www.freelamp.com
LAMP Architecture of the site, I have previously focused on the installation/configuration aspects of the development of relatively few, because they are engaged in the development of less. The text of this article is of course also from:

Published on the O ' Reilly Network (http://www.oreillynet.com/)
Http://www.oreillynet.com/pub/a/onlamp/2002/04/04/webdb.html

Looked after, quite enlightening, before the development of some problems encountered, solved. So translate it and share it with everyone.


1. Use of arrays in PHP
Using associative arrays (associatively-indexed arrays) is helpful when manipulating databases, so let's look at an array traversal of a basic number format:

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

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

Another way to save more code, however, is:

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

foreach can also output text subscript:

$temp = Array ("Club" = "Richmond",
"Nickname" = "tigers",
"Aim" = "premiers");

foreach ($temp as $key = $value)
echo "$key: $value";
?>
Approximately 50 functions for working with arrays are described in the PHP manual.

2. Add a variable to the PHP string

This is very simple:

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

However, it is necessary to note that, although there are no errors in the following example:
$temp = Array ("One" = 1, "one" = 2);
Output:: The first element is 1
echo "The first element is $temp [one].";
?>

However, if there is no double quotation mark in the back of the Echo statement, it is necessary to give an error, so we recommend using curly braces:

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


3. Accessing query results with associative arrays
Look at the following example:

$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\n";
}
?>

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

In multi-table checking, if the names of 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 referred to as: $row ["Wname"] and $row ["Rname"].


In the case of specifying table and column names, only column names are referenced:

SELECT winery.region_id
From Winery

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

A reference to a clustered function is the reference name:

SELECT COUNT (*)
from customer;

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

4. Note Common PHP bugs

The common PHP error correction problems are:

No page rendered by the Web browser if 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 this are not the logic of the script, but the bugs that exist in the HTML or the HTML generated by the script. For example, missing a similar,,If you close the Tag, the page cannot be refreshed. The solution to this problem is to look at the source code of the HTML.

For the complex, can not find the reason of the page, through the Web page verification program http://validator.w3.org/to analyze.

If a variable is not defined, or if the variable is defined incorrectly, the program becomes eccentric. For example, the following dead loop:

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

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

Error_reporting (E_all);

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

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

In many WEB database applications, some features often allow the user to click on a connection and continue to stay on the current page, so that the work I call it "single part query".

Here is a script called calling.php:

"-//W3C//DTD HTML 4.0 transitional//en"
"Http://www.w3.org/TR/html4/loose.dtd" >


<title>Calling Page Example</title>


Click here!



When the user clicks on the connection above, it calls action.php. Here is the source code for action.php:

Database Features

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

Here are two common mistakes to remind:
Call the header () function to include an exit statement to stop the script, or the subsequent script may output before the header is sent.


A common error with the header () function is:

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

The header () function can only be called before the HTML output, so you need to check for empty lines, spaces, and so on that may exist before PHP.

6. Problems and solutions of reload
I used to write PHP programs, often encountered when the page refresh, the database processing more than once.
Let's see 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" >


<title>Customer Insert</title>


I ' ve inserted the customer for you.


?>
Suppose we use this program with the following connection:

Http://www.freelamp.com/addcust.php?surname=Smith&firstname=Fred

If this request is submitted only once, OK, there will be no problem, but if you refresh multiple times, you will have multiple records inserted.
This problem can be solved by the header () function: The following is the new version of 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);
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" >


<title>Customer Insert</title>


I ' ve inserted the customer for you.


This way, the original page continues to refresh with no 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 to read and write, to improve the processing speed of the table.

8. Develop a quick script with mysql_unbuffered_query ()
This function can be used to replace the mysql_query () function, the main difference is that mysql_unbuffered_query () returns immediately after executing the query, without waiting or locking 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.

http://www.bkjia.com/PHPjc/315255.html www.bkjia.com true http://www.bkjia.com/PHPjc/315255.html techarticle Source: www.freelamp.com LAMP Architecture of the site, I previously focused on the installation/configuration, the relatively small development, because they are engaged in the development of less. The text of this article is of course ...

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