我有一個名爲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" => ""
));
}
嘗試'$這個 - >胸徑=新PDO(「mysql的:居屋....'然後使用'$這個 - >胸徑'not'$ dbh'! –
你應該在'$ dbh-> prepare'中得到錯誤,因爲它超出了函數的範圍。 –
@JeffPuckettII at prepare我得到沒有數據源被配置爲運行這個sql並提供。 ... – Ayan