2016-07-30 67 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)");