asp的rs的循环语句在php的处理办法

asp中的语句rs.movenext的语句,要取20行数据,它是这样的:

<%

dim conn,rs, sql
set conn=getconn()
set rs=getRs()

sql = "select top 20 * from [table] order by id desc"

n = 0
rs.open sql,conn,3,3
while not rs.eof
    n = n + 1
    if n < 20 then
        response.write "0" & cstr(n)
    else
        response.write cstr(n)
    end if

    rs.movenext
wend
rs.close

set rs=nothing
set conn=nothing
%>

但是在PHP中就没有这么复杂了:

select * from table limit 20;  //就这么简单

如果一句句地照asp来转码,似乎要这样:

$i=0;
while($r = mysql_fetch_row($x))
{
   //....
  if ($i>=20) break;
$i++;
}

或者:

while($r = mysql_fetch_row($x))
{
   //....
}

还有一种,每次下移20条记录,避免一直在开头的20条:

用 
do...while

$sql="";
$rs=mysql_query($sql);

先判断

<?
  if (empty($row_rs))
{
echo "没有数据";
}
do 
{
echo "$row_rs";
}
while ($row_rs=mysql_fetch_assoc($rs));
?>

注释:mysql_fetch_assoc() 和用 mysql_fetch_array() 加上第二个可选参数 MYSQL_ASSOC 完全相同。它仅仅返回关联数组。这也是 mysql_fetch_array() 初始的工作方式。

提示:如果在关联索引之外还需要数字索引,用 mysql_fetch_array()。

注释:本函数返回的字段名是区分大小写的。

例子

<?php
$con = mysql_connect("localhost", "hello", "321");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db",$con);
$sql = "SELECT * from Person WHERE Lastname='Adams'";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_assoc($result));

mysql_close($con);
?>

输出:

Array
(
[LastName] => Adams
[FirstName] => John
[City] => London
) 

 

如果用ADODB就是

<?
$sql="";
$rs=$conn->execute($sql);

  echo $rs->fields['字段'];
$rs->movenext();
?>

原文地址:https://www.cnblogs.com/yuanscn/p/11040469.html