2015-03-30 47 views
0

我正在按照教程创建基于OOP的登录系统。我做了所有相应的事情,但在创建pdo时我在第15行的DB.php文件中发生错误。无法找出这个错误的原因。在那里停留了一段时间。任何人都可以帮我解决这个错误。代码可能看起来很长,但它是我保证的一块蛋糕。有四个php文件。无法确定创建PDO对象时可能导致此错误的原因

1.init.php文件包含用于创建新PDO()对象的成分。

2.config.php文件用于从init.php文件获取数据,因为字符串以('mysql/host')类型传递给它并使用explode()函数从中提取数据。

2.DB.php文件用于连接数据库。 我得到的错误是

enter image description here

db.php中的文件:

class DB{ 
    private $_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/user'),Config::get('mysql/password')); 


      }catch(PDOException as $e){ 
       echo $e->getMessage(); 
      } 
    } 
    public static function getInstance(){ 
     if(!isset(self::$_instance)){ 
       self::$_instance=new DB(); 
     } 

      return self::$_instance; 
     } 

    } 

config.php文件:

class Config{ 

     public static function get($path){ 
      if($path){ 
       $config=$GLOBALS['config']; 
       $arr=explode('/',$path); 
       foreach($arr as $bit){ 
        if(isset($config[$bit])){ 

         $config=$config[$bit]; 

        } 
       } 
       return $config; 
      } 
     } 
    } 

的init.php文件:

session_start(); 

$GLOBALS['config']=array(
    'mysql'=>array(

     'host' => 'localhost', 
     'db' => 'login', 
     'user' => 'root', 
     'password' => '' 

    ) 

); 

spl_autoload_register(function($class){ 

    require_once 'c:/xampp/htdocs/login/classes/'.$class.'.php'; 

}); 

require_once 'c:/xampp/htdocs/login/function/sanitize.php'; 

index.php文件:

require_once 'c:/xampp/htdocs/login/core/init.php'; 

    DB::getInstance()->query('SELECT name FROM table WHERE id=1'); 
+1

catch(PDOException as $ e)' - > catch(PDOException $ e)'为什么要使用'as'?在这里没有意义(欲了解更多信息,请参阅:http://php.net/manual/en/language.exceptions.php) – Rizier123 2015-03-30 19:17:50

+1

删除'as'。这不是别名。 – 2015-03-30 19:20:24

+0

其中一个这些天我会有我自己的别名@ Fred-ii- – 2015-03-30 19:28:09

回答

0

你的错误信息是分析错误。这意味着PHP解释器/处理器/程序试图读取您的文件,但发现语法错误,不得不停止。如果你看DB.php的第15行(根据错误信息)

}catch(PDOException as $e){ 

你会看到这个问题。这不是有效的PHP语法 - 你可能想

}catch(PDOException $e){ 

PDOException位的是对异常处理代码的类类型提示 - 有没有必要使用as

+0

OP没有回应我们的任何评论,所以也许他们会听你的;-) – 2015-03-30 19:45:22

+0

@ Fred-ii-哈,我没注意到那里。我的一般互联网政策是忽略评论:) – 2015-03-30 19:46:19

+0

哈哈是的,我记得阿兰;-) – 2015-03-30 19:46:55

相关问题