我有一个名为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