title: Notice: Undefined variable: conn and Fatal error: Call to a member function prepare() on a non-object
body: <p>Hi All I get these two errors when trying to post to my database</p>
  
  <p>Notice: Undefined variable: conn in C:\xampp\htdocs\phptuts\sandbox\blog\admin\index.php on line 17</p>
  
  <p>Here is the code;</p>
  
  <pre><code>    &gt; &lt;?php 
      require '../functions.php';
      require '../db.php';
      $data = array();
  
  
      if ( $_SERVER['REQUEST_METHOD'] === 'POST' )  {
          $title = $_POST['title'];
          $body = $_POST['body'];
  
          if ( empty($title) || empty($body) ) {
              $data['status'] = 'Please fill out both inputs';
          } else {
              Blog\DB\query(
                  "INSERT INTO posts(title, body) VALUES(:title, :body)",
                  array('title' =&gt; $title, 'body' =&gt; $body), 
                  $conn); // line 17
          }
      }
  
  view('admin/create', $data);
  </code></pre>
  
  <p>and here is the second one</p>
  
  <p>Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs    \phptuts\sandbox\blog\db.php on line 26</p>
  
  <pre><code>function query($query, $bindings, $conn)
  {
      $stmt = $conn-&gt;prepare($query); //line 26
      return $stmt-&gt;execute($bindings);
  
  }
  </code></pre>
  
  <p>Hoping someone can help me with this as I was doing the php fundamentals course on tutsplus The Obligatory Blog: Part 4 found here <a href="https://tutsplus.com/lesson/the-obligatory-blog-part-4/" rel="nofollow">https://tutsplus.com/lesson/the-obligatory-blog-part-4/</a></p>
  
  <p>update here is the db.php file </p>
  
  <pre><code>&lt;?php namespace Blog\DB;  
  
  $config = array(
      'username' =&gt; 'root', 
      'password' =&gt; 'foobar',
      'database' =&gt; 'blog'
  );
  
  function connect($config)
  {
      try {
          $conn = new \PDO('mysql:host=localhost;dbname=' . $config['database'],
                      $config['username'],
                      $config['password']);
          $conn-&gt;setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  
          return $conn;
      } catch (Exception $e) {
          return false;
      }
  }
  
  function query($query, $bindings, $conn)
  {
      $stmt = $conn-&gt;prepare($query);
      $stmt-&gt;execute($bindings);
  
      return ($stmt-&gt;rowCount() &gt; 0) ? $stmt : false;
  
  }
  
  function get($tableName, $conn, $limit = 10)
  {
      try {
          $result = $conn-&gt;query("SELECT * FROM $tableName ORDER BY id DESC LIMIT $limit");
  
          return ( $result-&gt;rowCount() &gt; 0 )
              ? $result
              : false;
      } catch(Exception $e) {
          return false;
      }
  }
  
  
  function get_by_id($id, $conn)
  {
      $query = query(
          'SELECT * FROM posts WHERE id = :id LIMIT 1',
          array('id' =&gt; $id),
          $conn
      );
  
      if ( $query ) return $query-&gt;fetchAll();
      // else
  }
  </code></pre>