表中没有数据

时间:2012-04-27 17:47:18

标签: php mysql html

这个代码,我希望在表中显示数据,代码中没有错误,但我仍然有一个空表,即使查询具有我想要的结果,这里是更多理解的代码< / p>

<?php
$connectdb = mysql_connect('localhost','root','sara', true ) or die ("Not Connect");
 if (!$connectdb)
 {
    die('Could not connect :'. mysql_errno());
  }
 $selestdb  = mysql_select_db('iexa', $connectdb) or die ("not selected database");
  if (isset($_POST['examID'])) {
   $examID = $_POST['examID'];
   }
    echo $examID;
    echo "<br />";
   $query = mysql_query("SELECT  Question  , Choise_1  , Choise_2 , Choise_3 , Choise_4 , Correct_Answer
   FROM question_bank WHERE E_No='examID' ORDER BY Question asc") or die ("mysql error");
 echo "<table width='40%' border='1' cellpadding='5'>
  <tr>
    <td>Qusetion </td>
    <td>Choise 1</td>
    <td>Choise 2</td>
    <td>Choise 3</td>
    <td>Choise 4</td>
    <td>The correct answer</td>
    </tr>";
   echo $query;
  while ($row = mysql_fetch_assoc($query)){
 echo '
  <tr>
   <td>'.$row['Question'].'</td>
   <td>' .$row['Choise_1'].'</td>
    <td>' .$row['Choise_2'].'</td>
   <td>' .$row['Choise_3'].'</td>
   <td>' .$row['Choise_4'].'</td>
    <td>' .$row['Correct_Answer'].'</td>
    </tr>';
     };
     echo "</table>";
     mysql_close($connectdb);
     ?>

3 个答案:

答案 0 :(得分:1)

问题可能出在E_No='examID',应该是:

E_No='" . mysql_real_escape_string($_POST['examID']) . "'

如果它是整数值,您始终可以将其强制转换为整数。 查询看起来像:

"SELECT  Question  , Choise_1  , Choise_2 , Choise_3 , Choise_4 , Correct_Answer FROM question_bank WHERE E_No='" . mysql_real_escape_string($_POST['examID']) . "' ORDER BY Question asc"

答案 1 :(得分:1)

您正在搜索文字'examId'

试试这个:

$examID = mysql_real_escape_string($_POST['examID']);
$query = mysql_query("SELECT  Question  , Choise_1  , Choise_2 , Choise_3 , Choise_4 , Correct_Answer
FROM question_bank
WHERE E_No='$examID' ORDER BY Question asc");
#           ^ here

答案 2 :(得分:0)

您是否尝试过更换或死亡(“mysql错误”); with或die(mysql_error()); ?

看起来像考试ID一开始也需要$。