棘轮PHP致命错误:未定义的类常量' MYSQL_ATTR_INIT_COMMAND'在

时间:2017-03-07 03:44:38

标签: php mysql sql database websocket

我尝试将棘轮与mysql数据库连接,但我得到了这个erorr 我使用xampp phpmyadmin运行mysql并使用connectToDB.php文件中的代码之前使用php将数据从html表单发送到sql行并且运行正常 我在添加sql行之前测试了Chat.php文件中的代码,这样做很好但是在chat.php中添加了connectToDB.php之后我得到了这个错误,所以问题出在哪里? 错误

  

PHP致命错误:未定义的类常量' MYSQL_ATTR_INIT_COMMAND'   在第19行的htdocs / connectToDB.php中

connectToDB.php代码

<?php
if(!session_id()) session_start();



/* $dbhost  = 'localhost';
//$dbname       = '1852132_ch';
//$dbuser       = 'root';
//$dbpass       = ''; */

$dbhost     = "localhost";
$dbname     = "test";
$dbuser     = "root";
$dbpass     = "";

// database connection
try{
    $_db = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass, array(
                    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", 
                    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                    PDO::ATTR_PERSISTENT => true
                )); 
}catch(Excepion $e){
    die("ERROR : ".$e->getMessage());
}
?>

Chat.php文件代码

<?php
namespace MyApp;

include('/opt/lampp/htdocs/ww/classes/user.php');
include('/opt/lampp/htdocs/ww/connectToDB.php');

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        // Store the new connection to send messages to later
        $this->clients->attach($conn);

        echo "New connection! ({$conn->resourceId})\n";
        $sql = $_db->query("UPDATE users SET userid= '1999' WHERE UserName='batman'");

    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

        foreach ($this->clients as $client) {
            if ($from !== $client) {
                // The sender is not the receiver, send to each client connected
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        $this->clients->detach($conn);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }
}

1 个答案:

答案 0 :(得分:0)

在php.ini文件中,您应该有以下行(取消注释):

extension=php_pdo_mysql.dll on Windows
extension=php_pdo_mysql.so on Linux/Mac

然后重启apache并尝试代码