dongzhong1891 2017-10-03 20:38
浏览 148

从php中的select语法打印数组值

I have a database which have 4 records in it. And I want to display the record details of which having reg_no = "IT17052498" (I'm already having that record in the db). But it gives and error like

"Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\labsheet 10\addcomment.php on line 85"

Database connection are as followed name(db_connect.php)

<?php
$hostname = "localhost";
$username = "root";
$password = "";

$mysql_db = "iwt";

$connection = mysqli_connect($hostname,$username,$password,$mysql_db);
if(!$connection)
{
die("Error : ".mysqli_error($connection));
}

else
{
echo "Successfully Connected!<br>";
}
?>

Here is my php code..

<html>

<body>
<table border="1">
<tr>
<th>Registration Number</th>
<th>Student Name</th>
<th>Comment on ITA</th>
</tr>
<?php
if(isset($_POST["btn_search"]))
$srch = $_POST["search"];
$query = "SELECT * FROM  library_comment WHERE regNo = $srch";

$exst = mysqli_query($connection,$query);
while($row = mysqli_fetch_array($exst))
{

?>

<tr>
<td><?php echo $row['regNo'] ?></td>
<td><?php echo $row['name'] ?></td>
<td><?php echo $row['comment'] ?></td>
</tr>

<?php
}

?>


</table>
</body>

  • 写回答

1条回答 默认 最新

  • dongqiancui9194 2017-10-03 20:53
    关注

    I don't even use PHP, but the error message says the problem is in the first argument in the call to mysqli_fetch_array():

    mysqli_fetch_array($exst)
    

    Specifically, $exst is supposed to be a mysqli_result object, but it's actually a boolean (either true or false).

    $exst comes from this line:

    $exst = mysqli_query($connection,$query);
    

    http://php.net/mysqli_query says:

    Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object.

    In other words, mysqli_query is failing and returning false, and your code doesn't check for errors, it just assumes everything is OK. This is the immediate source of your problem.

    On another note, the documentation also says

    Data inside the query should be properly escaped.

    Your code doesn't escape anything. This might be why the query fails.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀