\Workerman\MySQL\Connection 查询异常

qq7467466

问题描述

webman自定义进程中process.php 想动态获取数据库配置,发现如果数据库里面如果有符合条件的内容那么页面就不会报错, 如果查询出来的数据为空, 程序就报错了, 代码实测是没有问题的, 是不是因为在process自定义进程中会出现一些莫名其妙的问题呢? 如果要处理的话应该如何去写呢

程序代码

global $kafkaConfig;
global $db;
if (empty($kafkaConfig)) {

    if (empty($db)) {
        $host = extraEnv('database.host') ?: '127.0.0.1';
        $port = extraEnv('database.port') ?: 3306;
        $username = extraEnv('database.username') ?: 'root';
        $password = extraEnv('database.password') ?: '';
        $database = extraEnv('database.database') ?: 'cloud_events';
        $db = new \Workerman\MySQL\Connection($host, $port, $username, $password, $database);
    }
    // $kafkaConfig = $db->select('*')
    //     ->from('cloud_kafka_config')
    //     ->where('status= :status')
    //     ->bindValues(array('status'=>'normal'))
    //     ->query();
    $kafkaConfig = $db->query("SELECT * FROM `cloud_kafka_config` WHERE status='normal'");
}

报错信息

Error: Call to a member function execute() on bool in /data/wwwroot/cloud-events_test/vendor/workerman/mysql/src/Connection.php:1746
Stack trace:
#0 /data/wwwroot/cloud-events_test/vendor/workerman/mysql/src/Connection.php(1828): Workerman\MySQL\Connection->execute()
#1 /data/wwwroot/cloud-events_test/config/plugin/ycgame/cloud-event/process.php(28): Workerman\MySQL\Connection->query()
#2 /data/wwwroot/cloud-events_test/vendor/workerman/webman-framework/src/Config.php(207): include('/data/wwwroot/c...')
#3 /data/wwwroot/cloud-events_test/vendor/workerman/webman-framework/src/Config.php(63): Webman\Config::loadFromDir()
#4 /data/wwwroot/cloud-events_test/vendor/workerman/webman-framework/src/support/App.php(141): Webman\Config::load()
#5 /data/wwwroot/cloud-events_test/support/bootstrap.php(48): support\App::loadAllConfig()
#6 /data/wwwroot/cloud-events_test/vendor/workerman/webman-framework/src/support/App.php(104): require_once('/data/wwwroot/c...')
#7 /data/wwwroot/cloud-events_test/vendor/workerman/workerman/Worker.php(2488): support\App::support\{closure}()
#8 /data/wwwroot/cloud-events_test/vendor/workerman/workerman/Worker.php(1627): Workerman\Worker->run()
#9 /data/wwwroot/cloud-events_test/vendor/workerman/workerman/Worker.php(1423): Workerman\Worker::forkOneWorkerForLinux()
#10 /data/wwwroot/cloud-events_test/vendor/workerman/workerman/Worker.php(1397): Workerman\Worker::forkWorkersForLinux()
#11 /data/wwwroot/cloud-events_test/vendor/workerman/workerman/Worker.php(560): Workerman\Worker::forkWorkers()
#12 /data/wwwroot/cloud-events_test/vendor/workerman/webman-framework/src/support/App.php(131): Workerman\Worker::runAll()
#13 /data/wwwroot/cloud-events_test/start.php(4): support\App::run()
#14 {main}

截图报错信息里报错文件相关代码

截图

886 2 0
2个回答

walkor 打赏

直接用pdo

  • 暂无评论
qq7467466

问题找到了, 我把if (empty($db)) 这个判断取消掉就好了

  • 暂无评论
年代过于久远,无法发表回答
×
🔝