-3

I don't know what the problem is with this line or how to fix it, before was okay and now I'm getting this error:

mysqli_fetch_object() expects parameter 1 to be mysqli_result

Here is my PHP code:

<?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;
      }
  }

?>

d.abyss
  • 204
  • 1
  • 4
  • 26
  • http://stackoverflow.com/q/2973202/1288198 – Ashwini Agarwal Apr 14 '14 at 12:16
  • You're very vulnerable towards SQL injection. Use prepared statements and bind variables or at *least* use `mysqli_real_escape_string($link, $variable)`. You're most likely getting this error because `$user->title` has a `'` in it, and you aren't escaping your variables. – h2ooooooo Apr 14 '14 at 12:16
  • why `{}` in your query? `user_id ='{$userid}'` – user2009750 Apr 14 '14 at 12:20
  • 3
    First off you need to find out which of your queries are causing the error.. From the quick look i took, you are fetching a mysqli object in 4 seperate instances. Once you have found the problem query, then edit your post and we can look into it in depth. – d.abyss Apr 14 '14 at 12:22
  • check if(!empty(fetchresult)){ }else{}, The problem is no data in db – Anand Somasekhar Apr 14 '14 at 12:26
  • To do what @danielsmile said, follow here: http://stackoverflow.com/a/22662582/285587 – Your Common Sense Apr 14 '14 at 12:28

2 Answers2

5

As far as your exact error is concerned one of your query is failing, the following steps might help. Ofcourse you question looks duplicate but here are some of the things that addresses your question

Your first query should be like this, with no curly braces, ofcourse untill you have explicitly ids wrapped in curly braces in your table.

SELECT * FROM user WHERE user_id ='$userid'

Secondly you are executing multiple queries so you might wanna consider error checking if your query executes properly or not(because of syntax error columns mismatch table name mismatch many more possibilities): do error checking like this as for while($a...) part

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";
}

Third i see your are getting pub_id make a comma seperated list of it so that you can give it as a parameter in your last query which is a long shot, why not use sub query for you IN clause like this:

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`;
user2009750
  • 3,169
  • 5
  • 35
  • 58
3

The error you are stating translates to this: The query fails somehow, instead of running the mysqli_query($link, $sorgu); line echo $sorgu, go to phpmyadmin and test your query, if it is bad, fix it in phpmyadmin until it works and set it up in the code correctly

ied3vil
  • 964
  • 1
  • 7
  • 18