title: Undefined variable: pdo
body: <p>I have try to  connect mysql database using pdo but it doesnot work and gives error as :</p>
  
  <pre><code>Undefined variable: pdo
  </code></pre>
  
  <p>where I have written the database query. </p>
  
  <p>Code to connect mysql database :</p>
  
  <pre><code>&lt;?php
  
  class Mysql
  {
     public function __construct() {
      try 
      {
        $pdo = new PDO ("mysql:host=localhost;dbname=db_project","root", "");
        $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
       } 
  
      catch (PDOException $e)
       {
          echo 'connection failed: '.$e-&gt;getMessage();
       }
  
      }   
  ?&gt;
  and the code where I have written the query is 
  
  &lt;?php
   include("mysql.php");
    class database extends Mysql
    {
  
    public function selectcheckusername($username)
    {
      $stmt = $pdo-&gt;prepare("SELECT * FROM `tbl_signup` WHERE username = '$username'");
      $stmt-&gt;bindParam(":username",$username, POD::PARAM_STR);
      $stmt-&gt;execute();  
    }
  
    public function insertTablelogin( $username, $password, $email, $address )
    {
       $stmt = $pdo-&gt;prepare("INSERT INTO `tbl_login`(`username`,`password`,`email`,`address`)
                VALUES('$username','$password','$email','$address')");
       $stmt-&gt;bindParam(":username",$username,":password",$password,":email",$email,":address",$address, POD::PARAM_STR);
       $query=$stmt-&gt;execute();
     }
  
     public function selectforgdv() 
     {
       $stmt = $pdo-&gt;prepare( "SELECT * FROM `tbl_login`");
       $stmt-&gt;bindParam(":userId",$userId,":username",$username,":password",$password,":email",$email,":address",$address ,PDO::PARAM_STR);
       $stmt-&gt;execute();
       echo "&lt;pre&gt;";
       print_r($stmt-&gt;fetchAll());
       echo "&lt;/pre&gt;";
  
      }
  
     public function selectforuser( $userId ) 
     {
      $stmt = $pdo-&gt;prepare("SELECT  * FROM `tbl_login` WHERE userId  = '$userId'");
      $stmt-&gt;bindParam(':userId',$userId,PDO::PARAM_INT);
      $stmt-&gt;execute();
      echo "&lt;pre&gt;";
      print_r($stmt-&gt;fetchAll());
      echo "&lt;/pre&gt;";
      }
  
     public function updatetablelogin($userId,$username_save,$password_save,$email_save,$address_save) {
         $stmt = $pdo-&gt;prepare("UPDATE `tbl_login` SET username ='$username_save', password ='$password_save',
                 email ='$email_save',address ='$address_save' WHERE userId = '$userId'");
         $smmt-&gt;execute();
         echo  $stmt-&gt;rowCount();
  
      }
  
    public function deletetablelogin($userId) {
       $stmt = $pdo-&gt;prepare("DELETE FROM `tbl_login` WHERE userId = '$userId'");
       $stmt-&gt;bindParam(':userId',$userId,PDO::PARAM_INT);
       $stmt-&gt;execute();
       echo $stmt-&gt;rowCount();
      }
   }
  
  ?&gt;
  </code></pre>