while循环外的PHP计数变量不会增加

时间:2014-08-20 16:43:12

标签: php mysql sql

我想从问题表中随机选择几个问题。但是,当我使用while循环打印出问题时,我想按升序回显计数器。但是计数器变量没有递增。我不知道我哪里出错了。

<php
$sql = "select id, questions, ans1, ans2, ans3, correctAns from questions Order By RAND() Limit 1";
$result = mysql_query($sql);
$result2 = mysql_num_rows($result);
for($x = 1; $x <= $result2; $x++)
{
while($row = mysql_fetch_array($result))
{
$id = $row['id'];
$quest = $row['questions'];
$a1 = $row['ans1'];
$a2 = $row['ans2'];
$a3 = $row['ans3'];
$correct = $row['correctAns'];

echo $x.'<br />';
echo $quest.<br />;
echo $a1.'<br />';
echo $a2.'<br />';
echo $a3.'<br />';
echo $correct.'<br />';
} //end while loop
} // end for loop
?>

计数器变量只是在循环中回显1作为值。

3 个答案:

答案 0 :(得分:2)

你把计数器放在第二个循环中:

<?php
$sql = "select id, questions, ans1, ans2, ans3, correctAns from questions Order By RAND() Limit 1";
$result = mysql_query($sql); //ADDED
$x = 1; // ADDED
while($row = mysql_fetch_array($result)) // CHANGED
{
  $id = $row['id'];
  $quest = $row['questions'];
  $a1 = $row['ans1'];
  $a2 = $row['ans2'];
  $a3 = $row['ans3'];
  $correct = $row['correctAns'];

  echo $x.'<br />';
  echo $quest.<br />;
  echo $a1.'<br />';
  echo $a2.'<br />';
  echo $a3.'<br />';
  echo $correct.'<br />';
  x++; //ADDED
} //end while loop
?>

此外,您必须将查询reusult传递给查询字符串的mysql_fetch_array insteach。
BTW,mysql_ *已被弃用。你应该使用mysqli_ *或PDO。

答案 1 :(得分:0)

这是因为你有一个循环数据库提取,将它合并到一个循环,试试这个:

<?php
  $sql = "select id, questions, ans1, ans2, ans3, correctAns from questions Order By RAND()";
  $result = mysql_num_rows($sql);
  $x = 1; // Start the counter
  while($row = mysql_fetch_array($sql))
  {
      echo $x.'<br />'
          .$row['questions'].'<br />'
          .$row['ans1'].'<br />'
          .$row['ans2'].'<br />'
          .$row['ans3'].'<br />'
          .$row['correctAns'].'<br />';
      $x++;
  } //end while loop
?>

将其缩短,效率略有提高。

答案 2 :(得分:0)

我想让大家明白,我感谢你们在努力解决我提出的挑战时所做的集体努力。但是,我已经解决了这个挑战。以下是来源:

<?php
// Establish Connection with Database// Select Database
include_once( '../../Connections/QUIZ.php' );
// Specify the query to execute

每个学生在登录后的会话中都有一个app id。 对于学生回答的每个问题,应用程序在另一个表u_answer中执行更新。 首先,我们将通过在u_answer表上执行选择查询来检查学生是否已经回答了任何问题。

AS_Id是会话变量中学生的ID。

$sql = "select * from u_answer,questions where u_answer.AS_Id = '" . $_SESSION[ 'appID' ] . "' and questions.qid = u_answer.qid";
$result = mysql_query( $sql, $con ) or die( mysql_error() );
$records = mysql_num_rows( $result );

if ( $records < 1 ) {
//create and increment a counter(x) by 1
$x = $records + 1;

对问题执行原始选择查询。

$sql2 = "select * from questions ORDER BY RAND() LIMIT 1";
else
{

使用找到的记录数将计数器增加1。

$x = $records + 1;

以下此选择查询可确保学生不会超过一次回答特定问题。

$sql2 = "SELECT questions.qid, questions.question,questions.ans1, 
questions.ans2, questions.ans3, questons.ans4, questions.correctAns
FROM questions WHERE NOT EXISTS(SELECT * FROM u_answer WHERE u_answer.qid = questions.qid AND u_answer.AS_Id = '" . $_SESSION[ 'appID' ] . "') ORDER BY RAND() LIMIT 1";   
$result2 = mysql_query( $sql2 );

此时执行while循环。

while ( $row = mysql_fetch_array( $result2 ) ) {
$id = $row['id'];
$quest = $row['questions'];
$a1 = $row['ans1'];
$a2 = $row['ans2'];
$a3 = $row['ans3'];
$correct = $row['correctAns'];

打印出提取的数据。

echo $x.'<br />'; //the counter variable.
echo $quest.<br />;
echo $a1.'<br />';
echo $a2.'<br />';
echo $a3.'<br />';
echo $correct.'<br />';
}

关闭与数据库的连接。

mysql_close($con);
?>

这适合我。