开发者

php mysql query within foreach loop not working

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 a开发者_StackOverflow社区gain)

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....


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜