title: How to fix 'Undefined variable: pdo; Fatal error: Uncaught Error: Call to a member function query() on null;'?
body: <p>I got stuck for hours in the code below. I don't know how I can fix this error.</p>
  
  <blockquote>
    <p>Notice: Undefined variable: pdo in C:\xampp\htdocs\latihan2\update.php on line 192
    Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\latihan2\update.php:192 Stack trace: #0 {main} thrown in C:\xampp\htdocs\latihan2\update.php on line 192</p>
  </blockquote>
  
  <pre><code>**//file config.php**
  
  
  &lt;?php
  /* Database credentials. Assuming you are running MySQL
  server with default setting (user 'root' with no password) */
  define('DB_SERVER', 'localhost');
  define('DB_USERNAME', 'root');
  define('DB_PASSWORD', '');
  define('DB_NAME', 'demo2');
  
  /* Attempt to connect to MySQL database */
  try{
      $pdo = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME,     DB_USERNAME, DB_PASSWORD);
      // Set the PDO error mode to exception
      $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  } catch(PDOException $e){
      die("ERROR: Could not connect. " . $e-&gt;getMessage());
  }
  ?&gt;
  
  
  **//file update.php line 186-236**
  
  
  &lt;?php  
      require_once "config.php";
  
      if(isset($_GET["id"]) &amp;&amp; !empty(trim($_GET["id"]))){
          $id =  trim($_GET["id"]);
          $data_det = "SELECT * FROM employees_det WHERE id = :id";
          $result_det = $pdo-&gt;query($data_det);
          if($result_det-&gt;rowCount() &gt; 0){
  
              echo "&lt;div class='table-responsive'&gt;";
              echo "&lt;table class='table table-bordered table-striped'&gt;";
                  echo "&lt;thead&gt;";
                      echo "&lt;tr&gt;";
                          echo "&lt;th&gt;ID Parent&lt;/th&gt;";
                          echo "&lt;th&gt;ID 2&lt;/th&gt;";
                          echo "&lt;th&gt;Name&lt;/th&gt;";
                          echo "&lt;th&gt;Job Level&lt;/th&gt;";
                          echo "&lt;th&gt;Address&lt;/th&gt;";
                          echo "&lt;th&gt;Salary&lt;/th&gt;";
                          echo "&lt;th&gt;Action&lt;/th&gt;";
                      echo "&lt;/tr&gt;";
                  echo "&lt;/thead&gt;";
                  echo "&lt;tbody&gt;";
  
              while($row = $result_det-&gt;fetch()){
                  echo "&lt;tr&gt;";
                      echo "&lt;td&gt;" . $row['id'] . "&lt;/td&gt;";
                      echo "&lt;td&gt;" . $row['id2'] . "&lt;/td&gt;";
                      echo "&lt;td&gt;" . $row['name'] . "&lt;/td&gt;";
                      echo "&lt;td&gt;" . $row['level'] . "&lt;/td&gt;";
                      echo "&lt;td&gt;" . $row['address'] . "&lt;/td&gt;";
                      echo "&lt;td&gt;" . $row['salary'] . "&lt;/td&gt;";
                  echo "&lt;/tr&gt;";
              }
                  echo "&lt;/tbody&gt;";                            
              echo "&lt;/table&gt;";
              echo "&lt;/div&gt;";
              // Free result set
              unset($result_det);
          } else{
              echo "&lt;p class='lead'&gt;&lt;em&gt;No records were found.&lt;/em&gt;&lt;/p&gt;";
          }
      }
      // Close connection
      unset($pdo);
  ?&gt;
  </code></pre>