【问题标题】:Object of class SQLite3Result could not be converted to intSQLite3Result 类的对象无法转换为 int
【发布时间】:2019-01-31 14:11:16
【问题描述】:

我想从 SQLite3 文件中的表 dictonary 中随机选择一列。但我不知道为什么它会给我一个警告。(注意)

这是我写的代码:(部分)

<?php
    class DB extends SQLite3{
        function __construct($path){
            return $this->open($path);
        }
        function randWord(){
            //Randomize select a word from database.
            $words = $this->query("select count(*) from 'dictonary';");
            return $this->query("select * from 'dictonary' limit 1 offset ".mt_rand(0,$words-1).";")->fetchArray()['wholeWord'];
        }
}
$database = new DB('storage.db');
echo json_encode([
    'status'=>200,
    'message'=>'Got an word!',
    'data'=>[
        'word'=>$database->randWord()
    ]
],JSON_PRETTY_PRINT);
$database->close();

这是回应:

<br />
<b>Notice</b>:  Object of class SQLite3Result could not be converted to int in <b>D:\phpStudy\htdocs\Files\word-match\api.php</b> on line <b>17</b><br />
{
    "status": 200,
    "message": "Got an word!",
    "data": {
        "word": "\u5b66\u795e\u4e4b\u5973"
    }
}

为什么?

【问题讨论】:

    标签: php select sqlite


    【解决方案1】:

    好的问题已经自己解决了。 $words 是一个对象,而不是一个数字。 我应该在下面使用这种方式来获取计数:

    $words['count(*)'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-02
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多