实际上我想使用php while sql从sql收集数据,如:
while ($res = mysql_fetch_assoc($dat)){//code here}
但是当我运行它时,它会发送sql中的所有记录。我只想显示有限的记录。怎么做。甚至还有一个问题,我可以将所有记录分成不同的页面吗?
答案 0 :(得分:5)
将limit
添加到查询中。 LIMIT
select * from your_table limit 10
或者为循环添加count
-
$count = 1;
while ($res = mysql_fetch_assoc($dat)){
if($count > 10) {
break;
}
// rest of the code
$count++:
}
答案 1 :(得分:2)
使用sql的限制功能。 例如:如果你想获取10条记录,那么你的查询应该是这样的:
SELECT * FROM table_name LIMIT 0,10;
其中'0'是起始值,'10'是上述查询中的记录数。
答案 2 :(得分:2)
mysqli_*
而非弃用mysql_*
你可以从这开始:
$result = mysqli_query($con,"SELECT * FROM Devices2 ORDER BY name");
$count = mysqli_num_rows($result); /* COUNT THE TOTAL ROW */
$rowsperpage = 20; /* NUMBER OF ROW PER PAGE */
$totalpages = ceil($count / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
$currentpage = (int) $_GET['currentpage'];
} else {
$currentpage = 1;
}
if ($currentpage > $totalpages) {
$currentpage = $totalpages;
}
if ($currentpage < 1) {
$currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
?>
<table><!-- START OF TABLE OF RECORDS -->
<?php
/* START OF SHOWING THE RECORD */
if($stmt = $con->prepare("SELECT name FROM Devices2 ORDER BY name LIMIT $offset, $rowsperpage")){
$stmt->execute();
$stmt->bind_result($name);
while($stmt->fetch()){
?>
<tr>
<td><?php echo $name; ?></td><!-- ROW OF RECORD -->
</tr>
<?php
} /* END OF WHILE LOOP */
$stmt->close();
} /* END OF PREPARED STATEMENT */
if($count == 0){ /* IF NO RECORD FOUND */
?>
</table>
<h1>No record found</h1>
<?php
}
else { /* ELSE, START THE PAGINATION LINK */
echo '<tr height="30px;" valign="bottom"><td>';
/* THIS IS THE SECOND TABLE FOR THE PAGINATION LINK */
echo "<table style='border-collapse:separate; border-spacing:3px;'><tr>";
/****** build the pagination links ******/
$range = 2;
if ($currentpage > 1) {
$prevpage = $currentpage - 1;
echo "<td style='width:70px; background-color:fff; border:solid #08c 1px; font-size:14px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage' style='background-color:fff;'>Previous</a> </td>";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
if (($x > 0) && ($x <= $totalpages)) {
if ($x == $currentpage) {
echo "<td style='width:20px; background-color:fff; font-size:14px; border:solid #ccc 2px;' align='center'> <font color='#ccc'><b>$x</b></font> </td>";
} else {
echo "<td style='width:20px; background-color:fff; font-size:14px; border:solid #08c 1px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?currentpage=$x' style='background-color:fff;'>$x</a> </td>";
}
}
}
if ($currentpage != $totalpages) {
$nextpage = $currentpage + 1;
echo "<td style='width:70px; background-color:fff; font-size:14px; border:solid #08c 1px;' align='center'> <a href='{$_SERVER['PHP_SELF']}?¤tpage=$nextpage' style='background-color:fff;'>Next</a> </td>";
} // end if
/****** end build pagination links ******/
echo "</tr></table></td></tr>";
} /* END OF ELSE IF COUNT 0 */
?>
</table>
答案 3 :(得分:1)
为此使用MYSQL LIMIT子句。
答案 4 :(得分:1)
你可以使用for循环也设置开始和结束限制。
for($i=$start ;$i< ($start + $limit) ; $i++)
{
//your code
}