2016-07-30 66 views
0

我有一個名爲chat.php與命名空間MyApp的文件,我試圖用PDO連接到數據庫,並插入一些數據,但在我的IDE我得到一個錯誤說方法執行找不到。我哪裏錯了?在PHP風暴給PDO執行所未發現的錯誤

以下是代碼的某些部分:

<?php 

namespace MyApp; 

use Ratchet\MessageComponentInterface; 
use Ratchet\ConnectionInterface; 
use Emojione\Client; 
use Emojione\Ruleset; 
use \PDO; 
use \PDOException; 

class Chat implements MessageComponentInterface { 
protected $clients; 
/** 
* @var \Emojione\Client 
*/ 
private $emojioneClient; 

public function __construct() { 
    $this->clients = new \SplObjectStorage; 
    /** 
    * Following for setting up conversion and display of native and ascii emojis 
    */ 
    $this->emojioneClient = new Client(new Ruleset()); 
    $this->emojioneClient->imageType = 'png'; 
    $this->emojioneClient->imagePathPNG = './assets/png/'; 
    $this->emojioneClient->ascii = true; 

    $this->connect(); 
} 
. 
. 
. 
. 
. 

public function connect() { 
    $hostname='localhost'; 
    $dbname = 'cryptoIM'; 
    $username='root'; 
    $password=''; 

    try { 
     $dbh = new PDO("mysql:host=$hostname; dbname=$dbname", $username, $password); 

     $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     echo 'Connected to Database<br/>'; 
    } 
    catch(PDOException $e) 
    { 
     echo ('ERROR: ' . $e->getMessage()); 
    } 
} 

public function insertData() { 
    $query = $dbh->prepare("INSERT INTO inbox(users, message, attachmentURI, timestamps) VALUES (:username, :messagetxt, :attachmentURI, :unixtime)"); 
    $query->execute(array(
     "username" => "", 
     "messagetxt" => "", 
     "attachmentURI" => "", 
     "unixtime" => "" 
    )); 
} 
+0

嘗試'$這個 - >胸徑=新PDO(「mysql的:居屋....'然後使用'$這個 - >胸徑'not'$ dbh'! –

+0

你應該在'$ dbh-> prepare'中得到錯誤,因爲它超出了函數的範圍。 –

+0

@JeffPuckettII at prepare我得到沒有數據源被配置爲運行這個sql並提供。 ... – Ayan

回答

1

顯然,$dbh不提供insertData()範圍。嘗試將$dbh添加到$this(類範圍),並且您必須將冒號:添加到​​數組鍵。

class Chat implements MessageComponentInterface { 
    protected $clients; 
    protected $dbh; 

則:

public function connect() { 
    //...  
    try { 
     $this->dbh = new PDO("mysql:host=$hostname; dbname=$dbname", $username, $password); 

     $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     echo 'Connected to Database<br/>'; 
    } 
    catch(PDOException $e) 
    { 
     echo ('ERROR: ' . $e->getMessage()); 
    } 
} 

則:

public function insertData() { 
    $query = $this->dbh->prepare("INSERT INTO inbox(users, message, attachmentURI, timestamps) VALUES (:username, :messagetxt, :attachmentURI, :unixtime)"); 
    $query->execute(array(
     ":username" => "", //Don't forget to add colons ':' 
     ":messagetxt" => "", 
     ":attachmentURI" => "", 
     ":unixtime" => "" 
    )); 
} 

關於錯誤execute方法沒有找到。我認爲你必須得到調用一個非對象或類似的東西的成員函數!

+0

如果你能告訴我一個你最後一段的意思,那麼這將有助於導致錯誤沒有消失 – Ayan

+0

另外一個注意事項!我可以看到你使用'MessageCo mponentInterface'是這個接口有你應該實現的任何方法! –

+0

是的,它具有已實施的方法。我根據你的回答更新了我的代碼,但我仍然有這個亮點。它會在未來發生任何重大錯誤嗎? – Ayan

1

因爲你要在你的類的不同函數中使用這個對象,那麼你需要聲明它是一個私有類變量。

class Chat implements MessageComponentInterface { 
protected $clients; 
private $dbh; 

然後在您的構造函數調用connect中初始化它。

try { 
    $this->dbh = new PDO("mysql:host=$hostname; dbname=$dbname", $username, $password); 

    $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    echo 'Connected to Database<br/>'; 
} 

和參考類屬性這樣別處:

public function insertData() { 
    $query = $this->dbh->prepare("INSERT INTO inbox(users, message, attachmentURI, timestamps) VALUES (:username, :messagetxt, :attachmentURI, :unixtime)");