当前位置: 首页 > 知识库问答 >
问题:

错误mysqli_fetch_array()期望参数1mysqli_result,字符串给定

龚德本
2023-03-14

警告:mysqli_fetch_array()要求参数1为mysqli_结果,字符串为给定值

这是我的密码谁能告诉我怎么了?

$result ="SELECT * FROM report" ;
if(mysqli_query($cons, $result)) {
echo("
<div class='sc'>
<table  id='to1' width='90%' border='0' cellspaceing='1' cellpadding='8' align='center'>
 <tr bgcolor='#9B7272'>
<td > ID </td>
<td > first_name </td>
<td > last_name </td>
<td > phone </td>
<td > address </td>
<td > email </td>
<td > birthdate </td>
<td > gender </td>
<td > city </td>
<td > dr_name </td>

</tr>

");
while($row = mysqli_fetch_array($result))
{
$ID         =$row['ID']; 
$first_name =$row['first_name'];
$last_name  =$row['last_name'];
$phone      =$row['phone'];
$address    =$row['address'];
$email      =$row['email'];
$birthdate  =$row['birthdate'];
$gender     =$row['gender'];
$city       =$row['city'];
$dr_name    =$row['dr_name'];

echo "  <tr bgcolor='#C7B8B8'>

共有3个答案

柳俊彦
2023-03-14

你可以这样写:-

$query = mysqli_query($cons,"SELECT * FROM items WHERE id = '$id'");
if (mysqli_num_rows($query) > 0)
{
while($row = mysqli_fetch_assoc($query))
{
  $result=$row;
}
}
韩宜春
2023-03-14

如注释中所述,您需要从查询中设置一个$结果,并在循环中使用它。

$qry ="SELECT * FROM report";
$result = mysqli_query($cons, $qry);
if ($result){
    while($row = mysqli_fetch_array($result)){
    }
}
公良扬
2023-03-14

问题

您缺少了如何将参数传递给mysqli_fetch_array()

解决方案

因此,这一行:

if(mysqli_query($cons, $result)) {

应该是

if($res = mysqli_query($cons, $result)) { // assign the return value of mysqli_query to $res

(FWIW,我会使用$res=mysqli\u查询($cons,$result);然后执行如果($res){

然后呢

while($row = mysqli_fetch_array($res)) // pass $res to mysqli_fetch_array instead of the query itself

为什么?

您将包含查询的字符串作为参数提供给mysqli\u fetch\u array()。这不是它的工作原理。您应该传递返回值mysqli\u query()。因此,您也可以编写:while($row=mysqli\u fetch\u array(mysqli\u query($cons,$result)){}(但它不是建议的,只是向您展示它的工作原理)。

 类似资料: