When it comes to displaying database data, it is generally done with a loop body, and the usual methods are two statements, while () and for (), and the following are some of their respective usages under different circumstances.
Let's take a separate presentation:
while () statement can display all the data, in the case of not knowing the number of cycles, especially convenient, and for () statement, can output display from the specified position to the end of the specified position of the data, the output display a certain range of data is used. Here's a look at the programming example:
We first build a database to spare: The database name is: MyDB table named: TBL.
Use the following statement: Create TABLE tal (idx int (3), url char (+), FreeText char (100))
You can use the phpMyAdmin tool to insert several data into a number of library tables.
Programming begins:
$id =mysql_connect ("localhost") or Die ("Database link cannot be established"); #链接数据库
$result =mysql_db_query ("MyDB", "SELECT * from TBL", $id); #查询结果并存入变量中
$rows =mysql_num_rows ($result); #得出数据表中的总行数, that is, the total number of data
Echo
"; #表格结束
In the above two sentences to insert the output statement, corresponding to different cases, the output statement is divided into several situations:
To output all data, first use for ()
for ($i =0; $i < $rows; $i + +) {
$total =mysql_fetch_array ($result);
echo "$total [FREETEXT] $total [IDX]";
}
Use while () to do
while ($total =mysql_fetch_array ($result))
{echo "$total [FREETEXT] $total [IDX]";
}
When we want to display the page, that is, not all the data can be displayed at once, then use for () to complete the task.
We assume that each output 10 data, with $page to represent the current number of pages $pagesize=10 to represent the amount of data on page. The statement is as follows:
for ($i =0; $i < $pagesize; $i + +)
{
$start = ($page-1) * $pagesize + $i; #计数起始的数据行数
if ($start < $rows)
$idx =mysql_result ($result, $start, "idx");
$url =mysql_result ($result, $start, "url");
$freetext =mysql_result ($result, $start, "freetext");
echo "$freetext $idx";
The above statement is used for () to separate the values of the fields in the data table into variables, which are displayed with the Echo statement.
The above program runs through the APACHE+MYSQL+PHP4
"The copyright of this article is owned by the author and house Orso near net, if need to reprint, please specify the author and source"
The above describes the efficacy and role of American ginseng tablets and methods of several display data comparison, including the efficacy and role of American ginseng tablets and the content of edible methods, I hope that the PHP tutorial interested in Friends have helped.