Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO连接未按要求工作_Php_Mysql_Pdo - Fatal编程技术网

Php PDO连接未按要求工作

Php PDO连接未按要求工作,php,mysql,pdo,Php,Mysql,Pdo,嗨,我正在使用PDO DB类连接到数据库。但我真的不知道我做得对不对。所有连接设置都正常,但运行查询时出现错误 我的目录结构是 /root /dbops <-- Directory contains `config.php` --> /dbfunctions <-- Directory contains `DBclass.php` & `DBFuncts.php` --> DBclass.php包含: class dbdunctions__DBclass

嗨,我正在使用PDO DB类连接到数据库。但我真的不知道我做得对不对。所有连接设置都正常,但运行查询时出现错误

我的目录结构是

/root
 /dbops <-- Directory contains `config.php` -->
   /dbfunctions <-- Directory contains `DBclass.php` & `DBFuncts.php` -->
DBclass.php
包含:

class dbdunctions__DBclass{
  public $instance = null;
  public function __construct() {}
  final private function __clone() {}

   public static function instance()
   {
        if (self::$instance === null)
        {
          $opt  = array(
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => TRUE,
            PDO::ATTR_STATEMENT_CLASS    => array('myPDOStatement'),
           );
            $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';
            charset='.DB_CHAR;
            self::$instance = new PDO($dsn, DB_USERNAME, DB_PASSWORD, 
            $opt);
        }
        return self::$instance;
   }
   public static function __callStatic($method, $args) {
    return call_user_func_array(array(self::instance(), $method), $args);
   }
}

class myPDOStatement extends PDOStatement
{
   function execute($data = array())
    {
       parent::execute($data);
       return $this;
    }
}
DBFuncts.php
包含以下内容:

class dbfunctions__DBFuncts
{
  protected $_con;
  public function __construct()
  {
    $db = new dbfunctions__DBclass();
    $this->_con = $db->con;
  }

  function gotodb(array $data){
    $result = 
  $this->_con::instance()->prepare($qry)->execute(array(/*parameters*/));
  }
}
现在,当使用
$result
触发查询时,我得到以下错误

分析错误:语法错误,第12行dbops/dbfunctions/DBFuncts.php中出现意外的“::”(T_PAAMAYIM_NEKUDOTAYIM)

请导游。我已经花了2个小时在这个问题上,在谷歌上搜索了一下。

而不是

$this->_con::instance()
你应该能够做到这一点

$this->_con->instance()->prepare($qry)->execute(array(/*parameters*/));
我不确定当你把代码放进去的时候它是否是一个打字错误,但是我注意到在DBclass.php中你有类dbduntions\uuu DBclass,当然这应该是类dbfunctions\uu DBclass()


另外,您的示例代码中似乎还有一些其他错误。。。但让我们一次解决一个问题:)

试着这样调整。我已经在我的服务器上进行了一些调整。值得注意的是,在
dbduntions\uu DBclass()
类的
\uu构造()
中实例化连接,并将
$this->\u con
分配给
self::$instance
dbduntions\uu DBclass:$instance;
):

PDO对象被创建,然后成为“成员对象”。gotodb对象使用“成员对象”PDO实例。下面是我正在工作的一个站点的代码示例,它应该有助于更好地解释它:

    try {
                $sql="
                    SELECT
                          id
                        , name
                        , description
                    FROM    
                        ue_bug_project                  
                ";
// $stmt objected created by preparing the SQL query for execution, using the PDO object, which in this case is $this->db
                $stmt = $this->db->prepare($sql);
// The execute method of the $stmt object is run executing the query, in this case no query data is bound as there is no user submitted data being used
                $stmt->execute();
// The result set is grabbed in one hit and placed into the projects array (I really should have set up the $projects variable as an empty array beforehand)
                $projects = $stmt->fetchAll(PDO::FETCH_ASSOC);
                return $projects;
            }
// If the query fails with an error, the catch block is run, in this case i log the error.
            catch (PDOException $e) {
                error_log('Error when getting the list of projects!');
                error_log(' Query with error: '.$sql);
                error_log(' Reason given:'.$e->getMessage()."\n");
                return false;
            }

}

从代码的外观来看,可能没有必要在PDO类周围放置自己的包装器

我想到了这一点,并改为现在的错误是
致命错误:对非对象调用成员函数实例()
$this->\u con=$db->con;尝试在DBFuncts.phpNow中设置$this->\u con=$db它是
致命错误:访问未声明的静态属性:Clws\uu DBclass::$instance
。。我在上面胡闹了2-3个小时:(但当我使用insert查询时,这种方法会失败…为什么?什么会失败?我认为有两个函数可以完成任务..一个是insert,另一个是select哦,是的,您需要创建一个select和insert函数,但连接应该可以正常工作,因为insert语句将始终返回0。这会对未定义的方法dbfunction进行错误调用。)s_uudbclass::prepare()@Cristian Bitoi…你能看到这个错误吗?它怎么可能是重复的。我的问题是完全不同的。为什么你要在PDO周围放置一个包装器?当我简单地包含这个文件时,这个PDO可以工作,但是当我尝试通过类时,我会面临问题。所以围绕PDO的包装器没有给出问题。如果你知道sol,请告诉我
class dbdunctions__DBclass
    {
        // Make the instance static
        public  static  $instance = null;

        public  function __construct()
            {
                // Create the static connection in the construct
                $this->init();
            }

        private function init()
            {
                if (self::$instance === null) {
                    $opt  = array(
                                PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                                PDO::ATTR_EMULATE_PREPARES   => TRUE,
                                PDO::ATTR_STATEMENT_CLASS    => array('myPDOStatement'),
                               );

                    self::$instance =   new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.'; charset='.DB_CHAR, DB_USERNAME, DB_PASSWORD, $opt);
                }
            }

        final private function __clone()
            {
            }

        public  static  function __callStatic($method, $args)
            {
                return call_user_func_array(array(self::instance(), $method), $args);
            }
    }

class myPDOStatement extends PDOStatement
    {
        public  function execute($data = array())
            {
                parent::execute($data);
                return $this;
            }
    }

class dbfunctions__DBFuncts
    {
        protected $_con;

        public function __construct()
            {
                // Create instance of database
                $database   =   new dbdunctions__DBclass();
                // Assign the connection to the $this->_con
                $this->_con =   dbdunctions__DBclass::$instance;
            }

        public  function gotodb($statement = false,$bind = false)
            {
                // Incase the statement or bind is empty, return 0
                if(empty($statement) || empty($bind))
                    return 0;
                // Create the query with method chain
                $query  =   $this   ->_con->prepare($statement)
                                    ->execute($bind);
                // Fetch results
                while($row = $query->fetch())
                    $result[]   =   $row;
                // If results return array else return 0 for consistency
                return (!empty($result))? $result : 0;
            }
    }

// Instantiate
$dbFunc =   new dbfunctions__DBFuncts();
// Use whatever you use to return a result here. This statement happens
// to work for my database...but likely not yours
print_r($dbFunc->gotodb("select * from `users` where `ID` = :ID",array(":ID"=>"29")));
class dbfunctions__DBFuncts
{
  protected $_con;
  public function __construct()
  {
    $db = new dbfunctions__DBclass();
    $this->db = $db;
  }

  function gotodb(array $data){
    $result = 
  $stmt = $this->db->prepare($qry);
$stmt->execute(array(/*parameters*/));
  }
    try {
                $sql="
                    SELECT
                          id
                        , name
                        , description
                    FROM    
                        ue_bug_project                  
                ";
// $stmt objected created by preparing the SQL query for execution, using the PDO object, which in this case is $this->db
                $stmt = $this->db->prepare($sql);
// The execute method of the $stmt object is run executing the query, in this case no query data is bound as there is no user submitted data being used
                $stmt->execute();
// The result set is grabbed in one hit and placed into the projects array (I really should have set up the $projects variable as an empty array beforehand)
                $projects = $stmt->fetchAll(PDO::FETCH_ASSOC);
                return $projects;
            }
// If the query fails with an error, the catch block is run, in this case i log the error.
            catch (PDOException $e) {
                error_log('Error when getting the list of projects!');
                error_log(' Query with error: '.$sql);
                error_log(' Reason given:'.$e->getMessage()."\n");
                return false;
            }

}