2014-01-16 154 views
0

所以我創建一個會員網站,到目前爲止我只有2類一類用於連接得到了數據庫和一流的管理用戶麻煩PDO對象

這裏是數據庫類:

class dbConnect 
{ 
    protected $db_conn; 
    public $db_host = '127.0.0.1'; 
    public $db_user = 'root'; 
    public $db_pass = ''; 
    public $db_name = 'db'; 

    public function connect() 
    { 
    try 
    { 
     $this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user,$this->db_pass); 
     return $this->db_conn; 
    } 
    catch(PDOException $e) 
    { 
     return $e->getMessage(); 
    } 
    } 

} 

下面是用戶級的,它只有1種方法我剛開始這個項目

class ManageUsers 
{ 
    public $link; 

    function __construct() 
    { 
    $db_connection = new dbConnect(); 
    $this->link = $db_connection->connect(); 
    return $this->link; 
    } 

    function registerUsers($username,$email,$password,$ip_adress,$date) 
    { 
    $query = $this->link->prepare("INSERT INTO `users` (username,email,password,ip_adress,date_joined) VALUES(?,?,?,?,?)"); 
    $values = [$username,$email,$password,$ip_adress,$date]; 
    $query->execute($values); 
    $confirm = $query->rowCount(); 
    return $confirm; 
    } 
} 

現在我只是運行一個測試對象,以確定這一切目前運行

$test = new ManageUsers(); 
echo $test->registerUsers('bob','[email protected]','lol','127.0.0.1','2012'); 

現在我收到錯誤,我正在調用prepare對象的非對象。說實話,我不明白,因爲我在構造方法中創建對象。以及任何建議幫助謝謝!

+0

取出返回$ this-> link;並看看這是否有效? –

+2

你在構造函數中返回'$ this-> link' - 你不需要這樣做,我懷疑這可能是你爲什麼會遇到問題。 – andrewsi

+0

@AbhikChakraborty謝謝,但不幸的是它沒有修復它 – swsa

回答

3

我認爲PDO對象是無效的,因爲你使用的是有對象的方法/屬性雙引號,你需要使用複雜的字符串表示法("{$object->property}")或.(點)

about php strings加入字符串的情況下

$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user,$this->db_pass); 

變化:

$this->db_conn = new PDO("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user,$this->db_pass); 
1

我從你這裏真的類似的腳本是

class dbConnect 
{ 
    protected $db_conn; 
    public $db_host = '127.0.0.1'; 
    public $db_user = 'root'; 
    public $db_pass = 'roadmin'; 
    public $db_name = 'todo'; 

    public function connect() 
    { 
    try 
    { 
     $this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user,$this->db_pass); 
     return $this->db_conn; 
    } 
    catch(PDOException $e) 
    { 
     return $e->getMessage(); 
    } 
    } 

} 

class ManageUsers 
{ 
    public $link; 

    function __construct() 
    { 
    $db_connection = new dbConnect(); 
    $this->link = $db_connection->connect(); 
    return $this->link; 
    } 

    function registerUsers($username,$email,$password,$ip_adress,$date) 
    { 
    $query = $this->link->prepare("INSERT INTO `users` (username,email, password, ip_adress, date_joined) VALUES(?,?,?,?,?)"); 
    $values = [$username,$email,$password,$ip_adress,$date]; 
    $query->execute($values); 
    $confirm = $query->rowCount(); 
    return $confirm; 
    } 

    function loginUser() 
    { 

    } 
} 

$test = new ManageUsers(); 
echo $test->registerUsers('bob','[email protected]','lol','127.0.0.1','2012'); 
+1

那麼,你修復了什麼?你發佈的代碼有什麼不同? –