dongzhi5846 2014-03-28 05:11
浏览 62

未定义的变量: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();
    }
 }

?>
  • 写回答

1条回答 默认 最新

  • douba4275 2014-03-28 05:15
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看