使用 LIMIT 显示 X 条记录数


displaying x amount of records using limit

我正在使用处理显示用户ID的查询,我遇到的问题是,它能够检索除最后一条记录之外的所有数据。例如,我有这些id(1,2,3,4,5)。使用我的脚本,它能够从 5 检索到 2 忽略 1。

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }
  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}
mysql_select_db($database_vivalooks, $vivalooks);
$query_rs_resq = "SELECT user_id FROM users_pics ORDER BY profile_id desc LIMIT 0,5; ";
$rs_resq = mysql_query($query_rs_resq, $vivalooks) or die(mysql_error());
$row_rs_resq = mysql_fetch_assoc($rs_resq);
$totalRows_rs_resq = mysql_num_rows($rs_resq);

HTLM & PHP

<table width="100%" border="0" cellspacing="4" cellpadding="4">
<?php
    if($totalRows_rs_resq > 0){ 
         while($row_rs_resq =  mysql_fetch_assoc($rs_resq)){ 
         $u_pic_id = $row_rs_resq['u_pic_id'];
?>
  <tr>
    <td><?php echo $row_rs_resq['u_pic_id']; ?></td>
  </tr>
<?php } ?>
</table>

使用我的脚本而不是显示 5 条记录,只显示 4 条记录。

看起来您

有两个语句执行相同的操作。

$row_rs_resq =  mysql_fetch_assoc($rs_resq);

删除其中一个,因为当它执行此操作时,它正在移动数组的内部指针。要么就是这样,要么重置内部指针。

请参见: mysql_fetch_assoc — 将结果行作为关联数组提取