title: What defines the PHP error: Call to a member function fetch_object() on a non-object
body: <p>I have been wondering on this question for a while.  I have two PHP programs that are almost exactly identical except for the fact that on of them is a function and the other isn't.  Furthermore one works and the other send back the error:</p>
  
  <pre><code>Call to a member function fetch_object() on a non-object
  </code></pre>
  
  <p>I fixed the one that wasn't working by omitting the variables and inserting definitive strings and adding <code>$con-&gt;errno</code> instead of <code>mysqli_errno</code>.  However, when I replaced the strings with the variables again the problem returned. </p>
  
  <p>So, my question is: what causes this error and how would I fix it.  Also, why is the error coming up in the second code and not the first?</p>
  
  <p><strong>The First Code (works)</strong></p>
  
  <pre><code>&lt;?php
  $con = new mysqli("database info");
  
  if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
  
   $stmt = $con-&gt;query("SELECT coordinator, announcements, description, comments, picture FROM Class_data WHERE class_year = '" .$class. "';");
  
      $result_row = $stmt-&gt;fetch_object();
  
      $coordinator = $result_row-&gt;coordinator;
      $announcement = $result_row-&gt;announcements;
      $description = $result_row-&gt;description;
      $comments = $result_row-&gt;comments;
      $picturepath = $result_row-&gt;picture;
  
      mysqli_error($con);
  
      mysqli_close($con);
  
      if ($picturepath == "")
      {
          $picturepath = "../images/AlumnLogo.png";
      }
  ?&gt;
  </code></pre>
  
  <p><strong>Second Code (doesn't work)</strong></p>
  
  <pre><code>&lt;?php
  
  function fetch($page, $content1, $content2, $content3)
  {
      $con = new mysqli("database info");
      if ($con-&gt;errno)
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
  
       $stmt = $con-&gt;query("SELECT '" .$content1. "' , '" .$content2. "' , '" .$content3. "' FROM '" .$page. "';");
  
      $result_row = $stmt-&gt;fetch_object();
  
      $content = array();
      $content[0] = $result_row-&gt;body;
      $content[1] = $result_row-&gt;calendar;
      $content[2] = $result_row-&gt;announcements;
      $output = implode("--",$content);
  
      mysqli_error($con);
  
      mysqli_close($con);
  
      return $output;
  }
  
  ?&gt;
  </code></pre>
  
  <p>Thanks alot!</p>