php mysqli_fetch_array() 期望参数 1 是 mysqli_result,null 给定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20576756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:18:01  来源:igfitidea点击:

mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given

phphtmlsql

提问by Mike_C

I'm getting an error that says "mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given" but everything looks good. What am I missing or adding here? It says the error is in Line 41. I appreciate you looking into this -- it's driving me crazy!

我收到一条错误消息,指出“mysqli_fetch_array() 期望参数 1 为 mysqli_result,给出 null”,但一切看起来都不错。我在这里缺少什么或添加了什么?它说错误在第 41 行。感谢您对此进行调查——这让我发疯!

<html>
<head>
<title>Search</title>
</head>

<body>
<h1>Search</h1>


<form method="post" action="search.php">
<input type="hidden" name="submitted" value="true" />

<label> Search | Category:
<select name="category">
<option value="name">Name</option>
<option value="date">Date</option>
</select>
</label>

<label>Search Criteria: <input type="text" name="criteria" /></label>
<input type="submit" />
</form>


<?php
if  (isset($_POST['submitted'])) {

// connect to DB
include('connect.php');

$category = $_POST['category'];
$criteria = $_POST['criteria'];
$query = "SELECT * FROM calls WHERE $category = '$category'";
$result = mysqli_query ($dbcon, $query) or die ('Error');


echo "<table>";
echo "<tr> <th>Date</th> <th>Name</th>";
}

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<tr><td>";
echo $row ['date'];
echo "<tr><td>";
echo $row ['name'];
}

?>
</body>
</html>

回答by Bryan Elliott

You have a "$" in your query..

您的查询中有一个“$”。

Should be:

应该:

SELECT * FROM calls WHERE category = '$category'

回答by apartridge

Proper indentation of your code will give you an indication of your problem. Notice that you do

代码的适当缩进将指示您的问题。注意你做

$result = mysqli_query ($dbcon, $query) or die ('Error');

Inside

里面

if  (isset($_POST['submitted'])) {

However,

然而,

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

is done outside this if-block (where $resultis null). Move the }down so it also contains the while.

在此 if 块之外完成(其中$resultnull)。}向下移动使其也包含while.

Also, you have a syntax error in your SQL query which will become apparent when you fix this first issue.

此外,您的 SQL 查询中存在语法错误,当您解决第一个问题时,该错误会变得很明显。