为什么不显示mysql表的信息?

时间:2013-04-22 00:33:49

标签: php mysql

未显示任何信息。得到错误,我真的不确定为什么? 感谢。

    <?php

    $mysqli = new mysqli("localhost", "root", "", "insuredcars");
     if ($_POST['formcar'] == '1' || $_POST['formage'] == '18' ||  $_POST['formNCD'] ==          '0' || $_POST['formPoints'] == '0' )
    $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '1'");
while($row = mysql_fetch_array($result))
 {
 echo "<tr>";
 echo "<td>" . $row['insuranceprice'] . "</td>";
 echo "</tr>";
  }
 echo "</table>";

 mysql_close($con);

 ?>

我得到的错误: 注意:未定义的变量:导致C:在第8行

警告:mysql_fetch_row()期望参数1为资源,在第8行的C:\ xampp \ htdocs \ search.php中给出null

注意:未定义的变量:C中的con:第16行

警告:mysql_close()期望参数1为资源,在第16行的C:中给出为null

2 个答案:

答案 0 :(得分:0)

mysqli_fetch_row只提取一行,如函数名称所示。

$mysqli = new mysqli("localhost", "root", "", "insuredcars");
if ($_POST['formcar'] == '1' || $_POST['formage'] == '18' || $_POST['formNCD'] == '0' || $_POST['formPoints'] == '0') {    
    $query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '1'");
    while ($row = mysqli_fetch_assoc($query)) {
        echo "<tr>";
        echo "<td>" . $row['insuranceprice'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}

mysql_close($con);

答案 1 :(得分:0)

尝试阅读有关使用mysqli_fetch_row而不是mysql_fetch_row的其他答案,但我认为您需要注意的是这一行:

$query = $mysqli->query("SELECT * FROM insurance WHERE insuranceid = '1'");

您将结果存储到$query变量中,但是当您循环时:

while($row = mysql_fetch_array($result))

您尝试获取$result变量,此变量未在此处声明。