2019-03-27 01:46
浏览 47


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.

    [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();
if($query->num_rows() > 0){
$result = $query->result();

Why do I get -1 as row count?



  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    解决 无用
    打赏 举报
  • dounou9751 2019-03-27 02:12

    This looks like it may be a bug in an older version of CodeIgniter.

    Try upgrading your version of CodeIgniter, or, if not possible, directly query the number of rows using a COUNT(*) query or using PHP's count() function on the full result set.

    解决 无用
    打赏 举报

相关推荐 更多相似问题