title: Fatal error. Call to member function prepare() on null in C:\xampp\htdocs\CRUD\read.php on line 29
body: <p>I have been following this tutorial on CRUD in php but I have come across an error that I have failed to intercept </p>
  
  <blockquote>
    <p>Fatal error. Call to member function prepare() on null in C:\xampp\htdocs\CRUD\read.php on line 29** and this error ion line 29 of my code is  **$stmt = $conn->prepare($query);</p>
  </blockquote>
  
  <p><code>read.php</code> file is this</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;  
  &lt;html&gt;  
  &lt;head&gt;
      &lt;title&gt;PDO - Read Records - -PHP CRUD Tutorial&lt;/title&gt;
  
      &lt;!--Bootstrap--&gt;
      &lt;link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css"&gt;
  
      &lt;script src="bootstrap/js/bootstrap.min.js"&gt;&lt;/script&gt;
       &lt;/head&gt; &lt;body&gt;
      &lt;div class="container"&gt;
          &lt;div class="page-header"&gt;
              &lt;h1&gt;Read Products&lt;/h1&gt;
          &lt;/div&gt;
          &lt;!--Dynamic content will go here--&gt;
  
          &lt;?php
  
          // include database connection
          include_once 'config/database.php';
  
          // select all data
          $query = "SELECT id, name, description, price FROM products ORDER BY id DESC";
  
          // prepare query for execution
          $stmt = $conn-&gt;prepare($query);
  
          // execute the query
          $stmt-&gt;execute();
  
          // this how to get number of rows returned
          $num = $stmt-&gt;rowCount();
  
          // link to create record form
          echo "&lt;a href='create.php' class='btn btn-primary m-b-1em'&gt;Create New Product&lt;/a&gt;";
  
          // check if more than 0 records found
          if($num &gt; 0) {
              echo "&lt;table class='table table-hover table-responsive table-bordered'&gt;"; // start table
  
              // creating our table heading
              echo "&lt;tr&gt;";
              echo "&lt;th&gt;ID&lt;/th&gt;";
              echo "&lt;th&gt;Name&lt;/th&gt;";
              echo "&lt;th&gt;Description&lt;/th&gt;";
              echo "&lt;th&gt;Price&lt;/th&gt;";
              echo "&lt;th&gt;Action&lt;/th&gt;";
              echo "&lt;/tr&gt;";
  
              // retrieve our table contents
              //fetch() is faster than fetchAll()
  
              while($row = $stmt-&gt;fetch(PDO::FETCH_ASSOC)) {
                  // extract row
                  // this will make $row['firstname'] to 
                  // just $firstname only
                  extract($row);
  
                  // creating new tablerow per record
                  echo "&lt;tr&gt;";
                      echo "&lt;td&gt;{$id}&lt;/td&gt;";
                      echo "&lt;td&gt;{$name}&lt;/td&gt;";
                      echo "&lt;td&gt;{$description}&lt;/td&gt;";
                      echo "&lt;td&gt;&amp;#36;{$price}&lt;/td&gt;";
                      echo "&lt;td&gt;";
  
                  // read one record
                  echo "&lt;a href='read_one.php?id={$id}'class=''btn btn-info m-r-1em'&gt;Read&lt;/a&gt;";
  
                  // we will use this link  to the next part of the post
                  echo "&lt;a href='update.php?id={$id}' class='btn btn-primary m-r-1em'&gt;Edit&lt;/a&gt;";
  
                  // we will use this link  to the next part of the post
                  echo "&lt;a href='#' onClick='delete_user({$id});' class='btn btn-danger'&gt;Delete&lt;/a&gt;";
  
                  echo "&lt;/td&gt;";
              echo "&lt;/tr&gt;";
              }
              //end table
          echo "&lt;/table&gt;"; 
  
          }
  
          // if no records found
          else{
              echo "&lt;div class='alert alert-danger'&gt;No records found.&lt;/div&gt;";
          }
          ?&gt;
  
      &lt;/div&gt;&lt;!--end of container--&gt;
  
      &lt;!--Jquery (necessary for bootstrap's javascript plugin)--&gt;
      &lt;script src="jquery-ui-bootstrap-jquery-ui-bootstrap-71f2e47/js/jquery-1.8.3.min.js"&gt;
   &lt;/script&gt; 
   &lt;/body&gt;  
  &lt;/html&gt;
  </code></pre>
  
  <p><code>database.php</code> file:</p>
  
  <pre><code>&lt;?php
      //variables used to connect to the database
      $servername = "localhost";
      $username = "root";
      $password = "";
      $dbname = "1phpbeginnercrudlevel1";
  
      //create a connection using the PDO extension
      try{
          $conn = new PDO("mysql:host=$servername;dbname=1phpbeginnercrudlevel1",$username,$password);
  
          //set the PDO error mode to exception
          echo "Connected successfully";
      }
      catch(PDOException $e)
      {
          echo "Connection failed: " .$e-&gt;getMessage();
      }
  
  
      ?&gt;
  </code></pre>