将 PDO 添加到我的代码时出错

发布于 2024-10-06 10:16:27 字数 888 浏览 4 评论 0原文

我有connection.php 文件,我正在$db 中初始化PDO。 我想在 User.php 中检查此验证,我将其包含在 connection.php 之后。 但这给了我错误。

try {
    $db = new PDO("mysql:dbname=$db_name;host=$db_host", $db_username,$db_password);
    echo "PDO connection object created";
}
catch(PDOException $e){
    echo $e->getMessage();
}

我如何通过执行 PDO 来验证此代码。 我如何将 PDO 传递给用户类..

 Fatal error: Call to a member function query() on a non-object in /var/www/youngib/rahul/yapi/user.php on line 41

    $sql="select * from users where email='$this->email'";
            $rs=$db->query($sql);
            if(mysql_num_rows($rs)>0){
                $msg=geterrormsg(4);
                    //email already exist
                echo $msg= "{   'success': 'false','msg':'$msg'     ,'error_code':'4'       }";
                return false;
            }

请帮助。

谢谢 。

I have connection.php file where i am initializing PDO in the $db.
And i want to check this validation in the User.php which i include after connection.php.
but it is giving me error .

try {
    $db = new PDO("mysql:dbname=$db_name;host=$db_host", $db_username,$db_password);
    echo "PDO connection object created";
}
catch(PDOException $e){
    echo $e->getMessage();
}

How can i validate this code by executing PDO.
How i will pass the PDO to the User Class..

 Fatal error: Call to a member function query() on a non-object in /var/www/youngib/rahul/yapi/user.php on line 41

    $sql="select * from users where email='$this->email'";
            $rs=$db->query($sql);
            if(mysql_num_rows($rs)>0){
                $msg=geterrormsg(4);
                    //email already exist
                echo $msg= "{   'success': 'false','msg':'$msg'     ,'error_code':'4'       }";
                return false;
            }

Please Help.

Thanks .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宛菡 2024-10-13 10:16:27

将其注入到类中或创建一个单例数据库类,例如...

注入:

class User
{
  protected $db;

  public function __construct(PDO $db)
  {
     $this->db = $db;
  }

  public function getDb()
  {
     return $this->db;
  }

  public function isUser($email)
  {
    $stmt = $this->getDb()->prepare('select count(email) as user_exists from users where email = :email');
    return (bool) $stmt->execute(array(':email' => $email))->fetchColumn();

  }
}

单例:

class Database {
   protected $pdo;
   protected static $instance;

  protected function __construct($dsn, $user, $password)
  {
     $this->pdo = new PDO($dsn, $user, $password);
  }

  public static function getInstance()
  {
     if(!self::$instance) 
     {
       // normally you would load the dsn, user, and password from a config file
       $db = Config::get('db');
       self::$instance = new self($db['dsn'], $db['user'], $db['password']);   
     }

     return self::$instance;
  }

  public function getDb()
  {

    return $this->pdo;
  }
}


class User
    {
      protected $db;

      public function __construct(PDO $db = null)
      {
         if(null !== $db)
         {
            $this->db = $db;
         }
      }

      public function getDb()
      {
         if(!$this->db)
         {
            $this->db = Database::getInstance()->getDb();
         }

         return $this->db;
      }

      public function isUser($email)
      {
        $stmt = $this->getDb()->prepare('select count(email) as user_exists from users where email = :email');
        return (bool) $stmt->exectute(array(':email' => $email))->fetchColumn();

      }
    }

Inject it in to the class or make a singleton DB class like...

Injection:

class User
{
  protected $db;

  public function __construct(PDO $db)
  {
     $this->db = $db;
  }

  public function getDb()
  {
     return $this->db;
  }

  public function isUser($email)
  {
    $stmt = $this->getDb()->prepare('select count(email) as user_exists from users where email = :email');
    return (bool) $stmt->execute(array(':email' => $email))->fetchColumn();

  }
}

Singleton:

class Database {
   protected $pdo;
   protected static $instance;

  protected function __construct($dsn, $user, $password)
  {
     $this->pdo = new PDO($dsn, $user, $password);
  }

  public static function getInstance()
  {
     if(!self::$instance) 
     {
       // normally you would load the dsn, user, and password from a config file
       $db = Config::get('db');
       self::$instance = new self($db['dsn'], $db['user'], $db['password']);   
     }

     return self::$instance;
  }

  public function getDb()
  {

    return $this->pdo;
  }
}


class User
    {
      protected $db;

      public function __construct(PDO $db = null)
      {
         if(null !== $db)
         {
            $this->db = $db;
         }
      }

      public function getDb()
      {
         if(!$this->db)
         {
            $this->db = Database::getInstance()->getDb();
         }

         return $this->db;
      }

      public function isUser($email)
      {
        $stmt = $this->getDb()->prepare('select count(email) as user_exists from users where email = :email');
        return (bool) $stmt->exectute(array(':email' => $email))->fetchColumn();

      }
    }
丶视觉 2024-10-13 10:16:27

我不想这么说,但尝试

global $db;

$db->query($sql); 行之前添加。它可能会起作用,具体取决于 $db 的创建位置。

也就是说,prodigitalson 的答案是一种极大改进的方法,它只涉及修复整个设计,这涉及更多的前期工作:)

I hate to say this, but try just adding

global $db;

before your $db->query($sql); line. It might work, depending on exactly where the $db was created.

That said, prodigitalson's answer is a vastly improved approach, it just involves fixing your entire design, which involves more up front work :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文