0

I'm trying to reset the data in my variable so I can call from it again. If there is data in the first query, I need it to run the reset. First I'm checking for data, and second, if there is data, I'm setting the index back to 0.

This works in my localhost environment, but not in my live environment. I know I'm getting the error I am getting because the first query isn't turning up any results, so mysql_num_rows($query) is empty.

I get the error: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource.

(This code works when the first query turns up results, successfully resetting and displaying the second query.)

if (mysql_num_rows($query)){
    mysql_data_seek($query, 0);
}

Is there a better way I can check if the first query has information, without spitting out an error if there isn't any information found?

4

1 回答 1

1

请注意有关使用 mysql_ 的其他评论?功能。

如果您出于某种原因要多次遍历结果,请按照 Marc B 的评论,在您的第一遍中将它们转储到一个数组中。例如(仍在使用您的 mysql_? 函数);

$result = mysql_query($query);
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
while($row = mysql_fetch_row($result)){
    $myResultSet[]=$row;
}

完成后,我们可以随心所欲地迭代该集合

foreach($myResultSet as $row){  # note foreach resets each time
    # do stuff with $row['fieldname']
}

但是,您应该借此机会将您的 mysql_ 更新为最新的方法……此基础解决方案将保持不变。

如果这不能解决问题,请发布更多代码,以便我们了解您尝试执行的原因和操作。

于 2012-09-11T21:45:48.887 回答