PHP致命错误:未捕获错误:在第23行中调用null上的成员函数query()

时间:2017-03-10 07:16:36

标签: php mysql websocket xampp composer-php

当我运行此行

$sql = $_db->query("UPDATE users SET theyear= '1999' WHERE UserName='batman'");

我收到此错误

PHP Fatal error:  Uncaught Error: Call to a member function query() on null in line 23 

我使用棘轮lib和php更改数据库行的值如果用户连接到服务器代码可以找到没有任何问题的数据库表但是当它来到编辑值行时我得到此错误

数据库connectToDB.php代码

$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());
}
?>

用于编辑行值的PHP代码

<?php
namespace MyApp;

include('/opt/lampp/htdocs/x/classes/user.php');
include('/opt/lampp/htdocs/x/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) {
    global $_db;

        // 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 theyear= '1999' WHERE UserName='batman'");

    }

composer.json代码

{
    "autoload": {
        "psr-0": {
            "MyApp": "src"
        }
    },
    "require": {
        "cboden/ratchet": "^0.3.6",
        "cboden/ratchet": "^0.3.6"
    }
}

1 个答案:

答案 0 :(得分:-1)

UICollectionViewFlowLayout* layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
layout.minimumLineSpacing = MESSAGE_BOX_MINIMUM_LINE_SPACING;
layout.estimatedItemSize = CGSizeMake(MESSAGE_BOX_WIDTH, MESSAGE_BOX_CELL_ESTIMATED_HEIGHT);
self.messageBox = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[self addSubview:self.messageBox];
self.messageBox.showsVerticalScrollIndicator = YES;
self.messageBox.showsHorizontalScrollIndicator = NO;
self.messageBox.backgroundColor = [UIColor clearColor];
[self.messageBox registerClass:[ZhiboCameraViewCell class] forCellWithReuseIdentifier:MESSAGE_BOX_CELL_IDENTIFIER];

这将起作用