user3471209
user3471209

Reputation: 1

Undefined variable: pdo

I have try to connect mysql database using pdo but it doesnot work and gives error as :

Undefined variable: pdo

where I have written the database query.

Code to connect mysql database :

<?php

class Mysql
{
   public function __construct() {
    try 
    {
      $pdo = new PDO ("mysql:host=localhost;dbname=db_project","root", "");
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

     } 

    catch (PDOException $e)
     {
        echo 'connection failed: '.$e->getMessage();
     }

    }   
?>
and the code where I have written the query is 

<?php
 include("mysql.php");
  class database extends Mysql
  {

  public function selectcheckusername($username)
  {
    $stmt = $pdo->prepare("SELECT * FROM `tbl_signup` WHERE username = '$username'");
    $stmt->bindParam(":username",$username, POD::PARAM_STR);
    $stmt->execute();  
  }

  public function insertTablelogin( $username, $password, $email, $address )
  {
     $stmt = $pdo->prepare("INSERT INTO `tbl_login`(`username`,`password`,`email`,`address`)
              VALUES('$username','$password','$email','$address')");
     $stmt->bindParam(":username",$username,":password",$password,":email",$email,":address",$address, POD::PARAM_STR);
     $query=$stmt->execute();
   }

   public function selectforgdv() 
   {
     $stmt = $pdo->prepare( "SELECT * FROM `tbl_login`");
     $stmt->bindParam(":userId",$userId,":username",$username,":password",$password,":email",$email,":address",$address ,PDO::PARAM_STR);
     $stmt->execute();
     echo "<pre>";
     print_r($stmt->fetchAll());
     echo "</pre>";

    }

   public function selectforuser( $userId ) 
   {
    $stmt = $pdo->prepare("SELECT  * FROM `tbl_login` WHERE userId  = '$userId'");
    $stmt->bindParam(':userId',$userId,PDO::PARAM_INT);
    $stmt->execute();
    echo "<pre>";
    print_r($stmt->fetchAll());
    echo "</pre>";
    }

   public function updatetablelogin($userId,$username_save,$password_save,$email_save,$address_save) {
       $stmt = $pdo->prepare("UPDATE `tbl_login` SET username ='$username_save', password ='$password_save',
               email ='$email_save',address ='$address_save' WHERE userId = '$userId'");
       $smmt->execute();
       echo  $stmt->rowCount();

    }

  public function deletetablelogin($userId) {
     $stmt = $pdo->prepare("DELETE FROM `tbl_login` WHERE userId = '$userId'");
     $stmt->bindParam(':userId',$userId,PDO::PARAM_INT);
     $stmt->execute();
     echo $stmt->rowCount();
    }
 }

?>

Upvotes: 0

Views: 18864

Answers (1)

elixenide
elixenide

Reputation: 44833

This is a scoping problem. $pdo only exists in your __construct method.

To make it available elsewhere, you need to make it a property of your class:

<?php

class Mysql
{
     var $pdo;
     public function __construct() {
     try 
     {
         $this->pdo = new PDO ("mysql:host=localhost;dbname=db_project","root", "");
         $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     } 
     catch (PDOException $e)
     {
         echo 'connection failed: '.$e->getMessage();
     }

}   
?>

Likewise, instead of $pdo, use $this->pdo in your database class.

Upvotes: 2

Related Questions