错误:mysqli_fetch_object() 期望参数 1 为 mysqli_result

Ahm*_*man -4 php mysql mysqli

我不知道这条线有什么问题或如何解决它,之前还好,现在我收到了这个错误:

mysqli_fetch_object() 期望参数 1 是 mysqli_result

这是我的PHP代码:

<?php
}   
if($_GET['action']=="user_info")
    {

    $userid = $_GET['user_id'];

    $query = "SELECT * FROM user WHERE user_id ='{$userid}'";
    $result = mysqli_query($link, $query);
    $user = mysqli_fetch_object($result);

    $queryt = "SELECT * FROM user_title WHERE id='".$user->title."'";
    $resultt = mysqli_query($link, $queryt);
    $rowt = mysqli_fetch_object($resultt);
    $title = $rowt->name;

        $sorgu = "select * from pub_author where user_id='$userid'";
        $publications = mysqli_query($link, $sorgu);

        while($a = mysqli_fetch_object($publications))
    {
       $ids .= $a->pub_id . ',';
    }

       $ids = rtrim($ids,",");
   $sorgu2 = "select count(id) as total , year from publication where id IN ($ids)
       GROUP BY YEAR(`year`) order by `year` ";
   $publications2 = mysqli_query($link, $sorgu2);

       while($a2 = mysqli_fetch_object($publications2))
       {
          $mount = explode('-', $a2->year);
          $accyaz[$mount[0]] = $a2->total;
      }
  }
Run Code Online (Sandbox Code Playgroud)

?>

ɢʜʘ*_*ɔʘɴ 5

就您的确切错误而言,您的查询之一失败了,以下步骤可能会有所帮助。当然,您的问题看起来很重复,但这里有一些可以解决您的问题

您的第一个查询应该是这样的,没有大括号,当然,直到您在表中显式地用大括号包裹了 id。

SELECT * FROM user WHERE user_id ='$userid'
Run Code Online (Sandbox Code Playgroud)

其次,您正在执行多个查询,因此您可能需要考虑检查您的查询是否正确执行的错误(因为语法错误列不匹配表名不匹配更多的可能性):像这样的错误检查while($a...)部分

if ($result=mysqli_query($link, $sorgu);)
{
  while($a=mysqli_fetch_object($result))
  {
    $ids .= $a->pub_id . ',';
  }

  $sorgu2 = "select count(id) as total , year from publication where id IN ($ids) GROUP BY YEAR(`year`) order by `year` ";

  //... Your further code
}
else
{
    echo "Something went wrong while executing query :: $sorgu";
}
Run Code Online (Sandbox Code Playgroud)

第三,我看到您正在pub_id制作一个逗号分隔的列表,以便您可以在最后一个查询中将其作为参数提供,这是一个很长的镜头,为什么不为您的IN子句使用子查询,如下所示:

SELECT 
    COUNT(id) as total, year 
    FROM publication 
    where id 
    IN (
        SELECT pub_id FROM pub_author WHERE user_id='$userid'
    ) 
    GROUP BY `year`
    order by `year`;
Run Code Online (Sandbox Code Playgroud)