简体   繁体   English

如何避免两次使用mysql_fetch_assoc

[英]How to avoid using mysql_fetch_assoc twice

Current page uses the: 当前页面使用:

$row_rsMyData = mysql_fetch_assoc (rsMyData);

As part of the mysql query created by Dreamweaver. 作为Dreamweaver创建的mysql查询的一部分。

Later on in the page I'm using: 在页面的后面,我正在使用:

while ($row_rsMyData = mysql_fetch_assoc($rsMyData))

To fetch filenames from the database to add to an array. 从数据库中获取文件名以添加到数组。

The problem is the first fetch gets the first records in the row and doesn't leave the data there for the second fetch meaning the result is an array with one record missing. 问题在于,第一次读取会获取行中的第一条记录,而不会在第二次读取中保留数据,这意味着结果是缺少一条记录的数组。

What could I use instead of the second fetch or is there a way to reset before the second one takes place. 我可以用什么代替第二次获取,或者在第二次获取之前有什么方法可以重置。

I have tried 我努力了

mysql_data_seek()

But it doesn't seem to work for me. 但这似乎对我不起作用。

The statement: 该声明:

  $row_rsMyData = mysql_fetch_assoc (rsMyData);

does not reference an array-type variable. 不引用数组类型的变量。 To gather multiple values into an array instead use: 要将多个值收集到数组中,请使用:

  $row_rsMyData[] = mysql_fetch_assoc (rsMyData);

Besides the fact that you should not be using mysql_* functions, you can just check the current value of $row_rsMyData in the loop condition, use those data, and then fetch the next row at the end of the loop: 除了不使用mysql_*函数外,还可以在循环条件下检查$row_rsMyData的当前值,使用这些数据,然后在循环结束时获取下一行:

while($row_rsMyData)
{
    //do stuff with the data in $row_rsMyData

    //fetch the next row
    $row_rsMyData = mysql_fetch_assoc($rsMyData);
}

This of course assumes you can't do anything about removing the line generated by Dreamweaver. 当然,这假设您无法删除Dreamweaver生成的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM