duanshanduo3363 2019-03-27 01:46
浏览 47
已采纳

使用查询num_row()得到-1的PHP代码Igniter

Im using below code to get result from database. Im expecting to get row count > 0 because I have 1 entry in database. Instead I'm getting the result below.

-1Array
(
    [0] => stdClass Object
        (
            [lngID] => 1
            [strRefTable] => team
            [intFirstParent] => 
            [intSecondParent] => 
            [bitHasChild] => 
            [strRefValue] => Automation
            [strShortDesc] => Automation
            [strLongDesc] => Automation
            [strCreatedBy] => 181551
            [dteCreatedDate] => 2019-03-27 09:09:08.000
            [strUpdatedBy] => 181551
            [dteUpdatedDate] => 2019-03-27 09:09:08.000
            [bitActive] => 1
        )

)

Where -1 represents the echo of $query->num_rows() and the array proves that I have record in database.

$query = $db->query($sql);
if(!$query) {
throw new Exception('Server error');
}
echo $query->num_rows();exit();
print_r($query->result());die();
if($query->num_rows() > 0){
$result = $query->result();
} 

Why do I get -1 as row count?

UPDATE

Using MSSQL

  • 写回答

2条回答 默认 最新

  • dongxianji0968 2019-03-27 09:55
    关注

    One possible explanation for this behavior (if you use PDO PHP Driver for SQL Server to connect to SQL Server), is that num_rows() uses PDOStatement::rowCount and based on documentation, the result will be -1 if you execute SELECT statement and do not use approipriate cursor type.

    Returns the number of rows added, deleted, or changed. If the last SQL statement executed by the associated PDOStatement was a SELECT statement, a PDO::CURSOR_FWDONLY cursor returns -1. A PDO::CURSOR_SCROLL ABLE cursor returns the number of rows in the result set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行