PHP Mysql Query无法抓取每个特定的行

时间:2013-10-12 03:03:49

标签: php mysql

我正在尝试抓取每一行的结果并一次打印出一个,这样我最终可以将它们放在自己的特定变量中,但它无法正常工作。谁能看到我做错了什么?它只是在无限循环中打印出第一行。我知道总共有4行。

$scores_query = mysql_query("SELECT Day, Time, AM_PM,
Away, 
case when Away_Score is null then '@' else Away_Score end as Away_Score, 
Home,
case when Home_Score is null then '' else Home_Score end as Home_Score
FROM `NFL_Games` WHERE Week=6 
order by
(case Day
 when 'Thu' then 1
 when 'Sat' then 2
 when 'Sun' then 3
 when 'Mon' then 4
 else 5 end), 
 Day, Time ASC");
 $count = mysql_num_rows($scores_query);
 echo "Hellloooo" . $count;
while($results = mysql_fetch_array($scores_query)) {
  for($x = 1; $x <= $count; $x++) {
    /*echo $results['Day'] . "__" . $results['Time'] . "__" . $results['AM_PM'] . "__" . $results['Away'] . "__" . $results['Away_Score'] . "__" . $results['Home'] . "__" . $results['Home_Score'];
    echo "<br/>";*/
    /*echo (Day, Time, AM_PM, Away, Away_Score, Home, Home_Score);*/
    if ($x = 1) {
    echo $results['Day'] . "__" . $results['Time'] . "__" . $results['AM_PM'] . "__" . $results['Away'] . "__" . $results['Away_Score'] . "__" . $results['Home'] . "__" . $results['Home_Score'];
    echo "<br/>";
    } else if ($x = 2) {
    echo $results['Day'] . "__" . $results['Time'] . "__" . $results['AM_PM'] . "__" . $results['Away'] . "__" . $results['Away_Score'] . "__" . $results['Home'] . "__" . $results['Home_Score'];
    echo "<br/>";
    } else if ($x = 3) {
    echo $results['Day'] . "__" . $results['Time'] . "__" . $results['AM_PM'] . "__" . $results['Away'] . "__" . $results['Away_Score'] . "__" . $results['Home'] . "__" . $results['Home_Score'];
    echo "<br/>";
    } else if ($x = 4) {
    echo $results['Day'] . "__" . $results['Time'] . "__" . $results['AM_PM'] . "__" . $results['Away'] . "__" . $results['Away_Score'] . "__" . $results['Home'] . "__" . $results['Home_Score'];
    echo "<br/>";
    } else {
    break;
    }
  }
}

2 个答案:

答案 0 :(得分:0)

看看你的if条款。 你真的想在if子句中为$ x分配值吗?

如果没有,请以这种方式编写if子句:if($ x == 2)

希望我理解你的问题。

答案 1 :(得分:0)

为什么你在while循环中进行for循环?由于你有if ($x = 1),每次运行for循环时$x都将设置为1,因此它将无限运行,并且永远不会离开for循环并运行while循环2~nth时间。

由于你没有在for循环中对$x做任何事情,只需执行你的while循环,它将迭代所有4行

while($results = mysql_fetch_array($scores_query)) {
    echo $results['Day'] . "__" . $results['Time'] . "__" . $results['AM_PM'] . "__" . $results['Away'] . "__" . $results['Away_Score'] . "__" . $results['Home'] . "__" . $results['Home_Score'];
    echo "<br/>";
}