我对数据库中的一个表有两个查询,唯一的区别是限制。我喜欢输出,但我知道一定有更实际的方法来做这件事。有没有人能提个建议?
$sql1 = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 20");
$count = mysql_num_rows($sql1);
if ($count > 0) {
while($row = mysql_fetch_array($sql1)){
}
}
$sql2 = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 1");
$count = mysql_num_rows($sql2);
if ($count > 0) {
while($row = mysql_fetch_array($sql2)){
}
}发布于 2015-06-24 22:08:18
创建一个函数并将限制作为参数传递:
function doQuery($limit = 1) {
$sql = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT $limit");
$count = mysql_num_rows($sql);
if ($count > 0) {
while($row = mysql_fetch_array($sql)){
}
}
};
doQuery(1);
doQuery(20);发布于 2015-06-24 22:09:08
$sql1 = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 20");
$count = mysql_num_rows($sql1);
if ($count > 0) {
while($row = mysql_fetch_array($sql1)){
//loop over all records...
}
}
$count = mysql_num_rows($sql1);
if ($count > 0) {
if($row = mysql_fetch_array($sql1)){
//retrieve one record
}
}https://stackoverflow.com/questions/31028771
复制相似问题