dongxu2398 2011-06-03 18:46
浏览 84
已采纳

foreach循环中的php mysql查询无法正常工作

Name pretty much explains it all--

I have an array and I want to run a query for each item in the array

Two variables come from this, volume and page number -- the volumes are contained within the array and I need to pull the respective page numbers for each volume

heres my code:

$volsArr = array(1, 2, 3, 4);

foreach ($volsArr as $volume) {
 $pages = mysql_query("SELECT page FROM $table WHERE vol = '$volume' LIMIT 1");
    if (!$pages) { die("Query to show fields from table failed"); }
      while($row = mysql_fetch_array($pages)) {
       $page = $row['page'];
      }
    echo 'volume is '.$volume.'';
    echo 'page is '.$page.'';
   }

I can't figure out what the issue I'm having is, I am getting an output like this:

Volume is 1 Page is 40

(this is correct for volume 1)

Volume is 2 Page is 40

(page should be 8)

Volume is 3 Page is 40

(page should be different again)

etc...

I tried unset($page) before closing the loop but that doesn't seem to do anything, any help, please!

And I also tried moving the echos so they are inside the while($row...) loop, but that will only provide me with output for the first item in the array....

  • 写回答

1条回答 默认 最新

  • doulachan8217 2011-06-03 18:49
    关注

    Based on your update, the reason is: Rows do not exist where vol in ('2','3','4')

    $page is never being updated because there are no rows to fetch.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥15 Onvif Device Manager提示“对操作"GetProfiles”的回复消息正文进行反序列化时出错”问题
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader
  • ¥45 谷歌浏览器出现开发者工具无法显示已创建的,但您可以调试已部署的代码。 状态代码 404, net::ERR HTTP RESPONSE CODE FAILURE
  • ¥15 如何解决蓝牙通话音频突发失真问题