简体   繁体   English

获取行数-mysqli

[英]get the number of rows - mysqli

I am trying this code but i want to know what is the number of results of this query. 我正在尝试此代码,但我想知道此查询的结果数是多少。 If do the same query in mysql console I get 4 results, but with this code I get 0 rows. 如果在mysql控制台中执行相同的查询,我将得到4个结果,但是使用此代码,我将得到0行。 What is the correct way to get the number of rows ? 获取行数的正确方法是什么?

 function check_oferta_existe($db, $id, $id_oferta) {
            $sql = $db -> prepare("
            SELECT COUNT(offer)
            FROM offer
            WHERE company_users_id_user1 = ? AND offer = ? AND state = 0
            ");

            $sql -> bind_param('is', $id, $oferta);
            $sql -> execute();
            $sql -> fetch();
            $sql->store_result();
            $rows = $sql->num_rows;

            var_dump($rows); // 0, should be 4
            if ($rows == 1) {
                return true;
            }
            else {
                return false;
            };
        }

$sql->fetch() is not a function - try $sql->fetch_all() . $sql->fetch()不是函数-尝试$sql->fetch_all()

That should fetch all the rows for you, and then the $sql->num_rows variable should be correct. 那应该为您获取所有行,然后$sql->num_rows变量应该正确。

I see two errors in your code. 我在您的代码中看到两个错误。 When you do this statement: 执行此语句时:

$sql -> bind_param('is', $id, $oferta);

you use $oferta but seems (looking at the function parameters) that the var should be $id_oferta . 您使用$oferta但是(从函数参数看)似乎var应该是$id_oferta

Moreover, after the closing bracket of your else you have a semicolon which should not be there. 此外,在else括号的后面有一个分号,不应放在该分号中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM