【问题标题】:PHP academy tutorial - query methodPHP学院教程-查询方法
【发布时间】:2013-11-01 14:08:41
【问题描述】:

我最近开始观看这一系列教程 (http://www.youtube.com/playlist?list=PLfdtiltiRHWF5Rhuk7k4UAU1_yLAZzhWc),并在编写 DB 类时遇到了问题。 这是第8个视频。

我的查询功能似乎无法正常工作。当显示“已连接”回声时,我可以成功连接到数据库。但是从不显示成功回声..

DB.php:

<?php
class DB {
    private static $_instance = null;
    private $_pdo, 
            $_query, 
            $_error = false, 
            $_results, 
            $_count = 0;

    private function __construct(){
        try{
            $db = new PDO('mysql:host='.config::get('mysql/host').';dbname='.config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
            echo '->Connected'.'<br />';
        }
        catch(PDOException $e){
            die($e->getMessage());  
        }
    }

    public static function getInstance() {
        if(!isset(self::$_instance)) {
            self::$_instance = new DB();
        }
        return self::$_instance;
    }

    public function query($sql, $params = array()){
        $this->_error = false;
        if($this->_query = $this->_pdo->prepare($sql)) {
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }
            if($this->_query->execute()){
                echo 'Success';
            }
        }
    }
}

你能发现我的错误吗?试图找到我的函数不起作用的地方,我设法理解查询函数的第一个 if 语句未验证.. 在 index.php 中,我使用与视频中相同的行:

DB::getInstance()->query('SELECT username FROM users WHERE username = ?', array('alex'));

所有数据库和表都已成功创建。我可以使用经典的 try catch bloc 和查询方法访问和执行 sql 查询。 我特别不明白的是查询函数中的第一个if语句条件。

感谢您的宝贵时间!!

【问题讨论】:

  • 你需要学习如何从 MySQL 获取错误信息。我确定这在教程中的某个地方。
  • if 语句检查$this-&gt;_query = $this-&gt;_pdo-&gt;prepare($sql) 是否成功

标签: php mysql sql youtube


【解决方案1】:

您的 PDO 对象存储在构造函数的局部变量 ($db) 中。而是将其分配给属性 ($_pdo),因为您的 query() 方法正在调用 $this-&gt;_pdo-&gt;prepare()

$this->_pdo = new PDO('mysql:host='.config::get('mysql/host').';dbname='.config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));

【讨论】:

    【解决方案2】:

    __construct() 中的问题

    错误:_construct()

    正确的是:__construct()

    私有函数 __construct() { 尝试 { $this->_pdo = new PDO('mysql:host='.Config::get('mysql/host').';dbname='.Config::get('mysql/db'), 配置::get('mysql/用户名'), 配置::get('mysql/密码') ); 回声“连接”; } 捕捉(PDOException $e){ 死($e->getMessage()); } }

    【讨论】:

      【解决方案3】:

      尝试复制/粘贴。

          <?php
      class DB {
          private static $_instance = null;
          private $_pdo, $_query, $_error = FALSE, $_results, $_Count = 0;
      
          private function __construct() {
              try {
                  $this -> _pdo = new PDO('mysql:host=' . config::get('mysql/host') . ';dbname=' . config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
                  echo "Conectat";
              } catch(PDOExeption $e) {
                  die($e -> getMessage());
              }
          }
      
          public static function getInstance() {
              if(!isset(self::$_instance)) {
                  self::$_instance = new DB();
              }
              return self::$_instance;
          }
      
          public function query($sql, $params = array()){
              $this->_error = false;
              if($this->_query = $this->_pdo->prepare($sql)) {                        
                  $x = 1;
                  if(count($params)){
                      foreach($params as $param){
                          $this->_query->bindValue($x, $param);
                          $x++;
                      }            }
                  if($this->_query->execute()){
                      echo 'Success';
                  }
              }
          }
      
      }
      

      【讨论】:

      • 为了让您的答案更好,您应该解释您所做的更改以及原始代码中的错误是什么(这毕竟是问题:))。这样,未来的其他用户可以从中学习,而不是解决特定用户的问题。
      猜你喜欢
      • 2023-03-31
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多