【问题标题】:How to catch empty results of a php mysqli query如何捕获 php mysqli 查询的空结果
【发布时间】:2013-04-23 01:15:19
【问题描述】:

这里是查询:

$stmt = $dbconnect->prepare("SELECT `title`,`description`,`postid` FROM `posttd` WHERE MATCH `title` AGAINST ( ? IN BOOLEAN MODE)");
$stmt->bind_param('s', $value);
$stmt->execute();

$value的值为'test1'、'other'和'test2'

值“其他”是一个 mysql 停用词。因此,当它通过查询时,它会产生任何结果。

只是想知道如何捕捉它,以便我可以将它从$value 数组中取出。

var_dump($stmt->execute()); 将在所有三个上给出bool(true)

我不希望在查询中运行 $value 之前尽可能过滤停用词。

var_dump($stmt)$stmt->execute(); 之后会产生以下结果:

test1 var_dump

object(mysqli_stmt)#2 (9) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(3) ["errno"]=> int(0) ["error"]=> string(0) "" ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) } 

test2 var_dump

object(mysqli_stmt)#2 (9) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(3) ["errno"]=> int(0) ["error"]=> string(0) "" ["sqlstate"]=> string(5) "00000" ["id"]=> int(3) } 

其他 var_dump

object(mysqli_stmt)#6 (9) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(3) ["errno"]=> int(0) ["error"]=> string(0) "" ["sqlstate"]=> string(5) "00000" ["id"]=> int(2) } 

唯一的区别是object(mysqli_stmt)#6

有什么想法吗?

【问题讨论】:

标签: php mysqli stop-words


【解决方案1】:

使用这个

 if($stmt->rowCount() > 0) {
   // do what you want here
}

上面的语句确保如果有数据,那么只有它会进入条件。

【讨论】:

  • rowCount() 是 pdo 对吗? mysqli版本是$stmt->num_rows,如果我是对的,我先去试试。
  • for mysqli try if ($stmt->field_count) { }
  • field_count 和 num_rows 都返回 true。我仍然无法捕捉到空的结果。还有其他想法吗?
  • 对于所有 $value 值甚至停用词,回显 field_count 的值都为 3
【解决方案2】:

你应该只使用

if($res = $stmt->get_result()) {
    //this means query returned some rows
}
else {
    //and this means the result was empty
}

而且,是的,它确实需要使用 mysqlnd(本机驱动程序)编译您的 php,否则您将收到未声明的 mysqli_stmt::get_result() 函数错误或类似的东西。

【讨论】:

    【解决方案3】:

    所以我无法使用内置函数捕捉它。相反,我做了一个简单的测试来捕捉空结果。这里是:

    $bindarray = array('+test1','+other','+test2');
    foreach($bindarray as $key=>$value)
    {
    echo $value; // NOTE1: This echo prints +test1, +other, and +test2
    $stmt = $dbconnect->prepare("SELECT `title`,`description`,`postid` FROM `posttd` WHERE MATCH `title` AGAINST ( ? IN BOOLEAN MODE)");
    $stmt->bind_param('s', $value);
    echo 'Bind:'.$value; // NOTE2: This echo prints +test1, +other, and +test2
    $stmt->execute();
    echo 'Execute:'.$value; // NOTE3: This echo prints +test1, +other, and +test2
    $stmt->bind_result($rtitle,$rdescription,$rpostid);
        while($stmt->fetch())
        {
            echo 'Fetch:'.$value; // NOTE4: This echo prints +test1 and +test2 ONLY
            if(!empty($value))
            {
                if(!in_array($value, $goodvalues))
                {
                    array_push($goodvalues,$value);
                }
            }
        }
    }
    

    正如您在 NOTE4 上看到的那样,$value 没有获取任何内容 ($stmt->fetch()),因此它没有通过 while 循环,因此它没有包含在 $goodvalues 中数组,所以我能够将停用词与数组分开。

    如果您能够使用内置函数捕获停用词,请。分享。

    谢谢

    【讨论】:

      猜你喜欢
      • 2011-08-21
      • 1970-01-01
      • 2019-03-08
      • 2016-11-03
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多