Comparison of several methods for displaying data

Source: Internet
Author: User
Tags comparison insert table name
Compare | data | show

When it comes to displaying database data, it is generally done with a loop body, and the common method has a while () and a for () two statements, and here's how they use them in different situations.
We will introduce each:
while () statement is able to display all the data, without knowing the number of cycles, especially convenient, and for () statement, you can output display from the specified position to the end of the specified position of data, in the output to display a certain range of data to use. Let's take a look at the programming example:
We first build a database to spare: The database name is: MyDB table name: TBL.
Use the following statement: Create TABLE tal (idx int (3), url char (m), FreeText char (100))
You can use the phpMyAdmin tool to insert several data into a number of library tables.
Programming Start:

$id =mysql_connect ("localhost") or Die ("Unable to establish a database link"); #链接数据库
$result =mysql_db_query ("MyDB", "SELECT * from TBL", $id); #查询结果并存入变量中
$rows =mysql_num_rows ($result); #得出数据表中的总行数, which is the total number of data
echo "<table>"; #准备以表格的形式输出
echo "</table>"; #表格结束

In the above two sentences insert output statements, corresponding to different situations, output statements are divided into several cases:
To output all data, use for () first

for ($i =0; $i < $rows; $i + +) {
$total =mysql_fetch_array ($result);
echo "<tr><td bgcolor=yellow><a href=" http://$total [url] "target=_blank> $total [freetext]</a > $total [idx]</tr></td> ";
}
Use a while () to do
while ($total =mysql_fetch_array ($result))
{echo ' <tr><td bgcolor=yellow><a href= http://$total [url] ' target=_blank> $total [freetext]</a > $total [idx]</tr></td> ";
}

When we want to page the display, that is, not all of the data at once to the end of the display, then you can use for () to complete the task.
We assume that each output of 10 data, using $page to represent the current number of pages $pagesize=10 to represent the amount of data on page. The statement looks like this:

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 "<tr><td bgcolor=yellow><a href=" http://$url target=_blank> $freetext </a> $idx </tr ></td> ";

The preceding statement is used for () to derive the values of each field in the datasheet into the variable, which is displayed with the Echo statement.

The above program runs through the APACHE+MYSQL+PHP4




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.