dsfgds4215 2018-11-07 16:59
浏览 37

从特定行启动mysqli_fetch_array

I have some records, which I use while($row = $result->fetch_assoc() to iterate on each one of them, then I get some other data from a different table using while($row2 = $result2->fetch_assoc(), that is, iterating also on each one of them, then displaying in a HTML table: part of first table data and part of second table data.

However, when I truncate the first table, and then insert new records, the second query $result2->fetch_assoc(), starts from the beginning of table and iterates X times, which is basically numbers of rows from first table. This is not what I want, I want to remember the last place of iteration from its table (table 2), then, when called again, only iterate the remain rows in the second table, always which is dependent on nth times from first table.

I found an answer in stackoverflow, which you can find it here, however, I didn't understand it correctly: how can you save last LIMIT value, so to start from X id if the $result2->fetch_assoc()is called again?

I thought about storing a counter in a text document (which is incremented by first while loop), then use LIMIT from that certain number, but I don't really get how to get it work.

Edit: here are some additional info:

Table "aplikimet" schema:

first table schema

Table "aplikimet_2" schema:

second table schema

$sql = "SELECT id, emri, mbiemri, email, telefoni, vendbanimi, datelindja, mesazhi FROM aplikimet";
$sql2 = "SELECT statusi, uid FROM aplikimet_2";
$result = $conn->query($sql);
$result2 = $conn->query($sql2);
if (($result->num_rows > 0) AND ($result2->num_rows>0)){
(html table and th are here)
 while((($row = $result->fetch_assoc()) AND $row2 = $result2->fetch_assoc()){
  (html td are here)

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • douyan5481 2018-11-07 17:19
    关注

    every time you call fetch_assoc() an internal pointer is incremented. after last element the call returns false so your while(...) loop will end.

    to reset it you can call

    mysqli_data_seek($result, 0);
    

    or

    $result->data_seek(0);
    

    see here

    IMHO is not a great way to do it. If you want to loop multiple times the same rowset you can save it in an array after the first complete loop. Then loop with foreach() your array all the times you need (your connection can be already closed at that time)

    To limit the number of the rows returned by your query use the SQL LIMIT clause which can have different syntax depending on RDBMS you are using.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看