gatewayworker连接mysql后该怎么判断表中是否存在某条数据

sunkkkk

使用中发现,如果数据库中查找到数据可以正常运行,没有查找到符合条件的数据会直接报错。
$result=$db->select('id')->from('collect_device')->where("unique_identification_number= $fromid ")->single();
echo $result;

885 1 0
1个回答

li914

报什么错误提示?

  • sunkkkk 2022-03-31

    Press Ctrl-C to quit. Start success.
    1PDOException: SQL:SELECT id FROM collect_device WHERE unique_identification_number= user SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user' in 'where clause' in C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\Applications\YourApp\Connection.php:1774
    Stack trace:

    0 C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\Applications\YourApp\Connection.php(1954): Workerman\MySQL\Connection->execute('SELECT id FROM ...', Array)

    1 C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\Applications\YourApp\Events.php(81): Workerman\MySQL\Connection->single()

    2 [internal function]: Events::onMessage('7f0000010b54000...', '{"type":"bind",...')

    3 C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\vendor\workerman\gateway-worker-for-win\src\BusinessWorker.php(384): call_user_func('Events::onMessa...', '7f0000010b54000...', '{"type":"bind",...')

    4 [internal function]: GatewayWorker\BusinessWorker->onGatewayMessage(Object(Workerman\Connection\AsyncTcpConnection), Array)

    5 C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\vendor\workerman\workerman-for-win\Connection\TcpConnection.php(629): call_user_func(Array, Object(Workerman\Connection\AsyncTcpConnection), Array)

    6 [internal function]: Workerman\Connection\TcpConnection->baseRead(Resource id #28)

    7 C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\vendor\workerman\workerman-for-win\Events\Select.php(286): call_user_func_array(Array, Array)

    8 C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\vendor\workerman\workerman-for-win\Worker.php(856): Workerman\Events\Select->loop()

    9 C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\vendor\workerman\gateway-worker-for-win\src\BusinessWorker.php(190): Workerman\Worker->run()

    10 C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\vendor\workerman\workerman-for-win\Worker.php(473): GatewayWorker\BusinessWorker->run()

    11 C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\vendor\workerman\workerman-for-win\Worker.php(382): Workerman\Worker::runAllWorkers()

    12 C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\Applications\YourApp\start_businessworker.php(35): Workerman\Worker::runAll()

    13 {main}

    process C:\phpStudy\PHPTutorial\WWW\kaiwu\vendor\GatewayWorker-for-win\Applications\YourApp\start_businessworker.php terminated and try to restart
    报了这一堆

  • li914 2022-03-31

    SELECT id FROM collect_device WHERE unique_identification_number= user 语句感觉有点奇怪 这个 user 是什么啊?
    unique_identification_number 这个是int型字段string字段类型?

  • li914 2022-03-31

    报的错误是 collect_device 这个表中无 user 字段

  • sunkkkk 2022-03-31

    我要在表中查找unique_identification_number(varchar)的值为user的一条数据

  • li914 2022-03-31

    哥 加单引号才可以 不然数据库会认为 user是字段名,不是 值

  • li914 2022-03-31

    $db->select('ID')->from('Persons')->where("sex= 'F' ")->single(); 例如这样子

  • sunkkkk 2022-03-31

    getawayworker数据查询查询不到的时候会直接报错吗,我想知道我遇到的这个错误正不正常。有没有办法判断表中是不是已经存在某条数据。谢谢了

  • li914 2022-03-31

    不会,你这是SQL语句错误

  • li914 2022-03-31
      $result=$db->select('id')->from('collect_device')->where("unique_identification_number= '{$fromid}' ")->single();  这样子写语句,应该不会报错了 
  • li914 2022-03-31

    然后再根据查询的结果判断是否存在

  • sunkkkk 2022-03-31

    没有报错了,多谢。因为我在代码里那个位置添的是$fromid,所以没加''.我直到现在也不是很明白,我加的$fromid确实是个变量。按照你说的问题确实解决了,万分感谢

  • 静默 2022-03-31

    这样写容易sql注入,最好按照手册 bindValues 那样传参数

  • li914 2022-03-31

    $fromid 如果不加单引号,会直接认为是一个 字段名称的

  • sunkkkk 2022-03-31

    明白了

年代过于久远,无法发表回答
×
🔝