注意:未识别的索引:C:\\ xampp \ htdocs \中的QNo,

时间:2014-09-27 10:46:56

标签: php mysql sql database

我正在尝试从SQL数据库($ demo)存储变量($ q11),但我收到错误

注意:未识别的索引:C:\ xampp \ htdocs \中的QNo,......

请一些人为此错误提供解决方案

<?php
$connect = mysql_connect("localhost","root","")
or die(mysql_error());
$sel=mysql_select_db("demo");

$query1 = mysql_query("SELECT * FROM `linuxq`  ORDER BY RAND() LIMIT 10 ");

echo "<h4 align='Center'><u>Linux Questions</u><br></h4>";

$rows11 = mysql_fetch_array($query1);
$q11 = $rows11['QNo'];
$qus11 = $rows11['Question'];
$a = $rows11['Opt1'];
$b = $rows11['Opt2'];
$c = $rows11['Opt3'];
$d = $rows11['Opt4'];
$ans11 = $rows11['Ans'];


echo " <b>Question:-<br></b>$qus11 <br>";
echo " <input type=radio name = 'answer$q11' value = '$a'></input>$a &nbsp &nbsp"; 
echo " <input type=radio name = 'answer$q11' value = '$b'></input>$b &nbsp &nbsp"; 
echo " <input type=radio name = 'answer$q11' value = '$c'></input>$c &nbsp &nbsp "; 
echo " <input type=radio name = 'answer$q11' value = '$d'></input>$d <br><br> ";

2 个答案:

答案 0 :(得分:0)

请尝试以下方法: -

$q11 = $rows11[0]['QNo'];
$qus11 = $rows11[0]['Question'];
$a = $rows11[0]['Opt1'];
$b = $rows11[0]['Opt2'];
$c = $rows11[0]['Opt3'];
$d = $rows11[0]['Opt4'];
$ans11 = $rows11[0]['Ans'];

答案 1 :(得分:0)

无论如何要避免不需要的错误消息,您应该通过添加例如以下内容来检查查询的记录数:

...
$query1 = mysql_query("SELECT * FROM `linuxq`  ORDER BY RAND() LIMIT 10 ");
echo "<h4 align='Center'><u>Linux Questions</u><br></h4>";

if(mysql_num_rows($query1)>0) {      
  $rows11 = mysql_fetch_array($query1);
  $q11 = $rows11['QNo'];
  $qus11 = $rows11['Question'];
  $a = $rows11['Opt1'];
  $b = $rows11['Opt2'];
  $c = $rows11['Opt3'];
  $d = $rows11['Opt4'];
  $ans11 = $rows11['Ans'];

  echo " <b>Question:-<br></b>$qus11 <br>";
  echo " <input type=radio name = 'answer$q11' value = '$a'></input>$a &nbsp &nbsp"; 
  echo " <input type=radio name = 'answer$q11' value = '$b'></input>$b &nbsp &nbsp"; 
  echo " <input type=radio name = 'answer$q11' value = '$c'></input>$c &nbsp &nbsp "; 
  echo " <input type=radio name = 'answer$q11' value = '$d'></input>$d <br><br> ";
}
...

此外我想你在桌子上有几个问题,但我没有看到任何循环,也许你忘了发布它......