简体   繁体   English

这个功能怎么了?

[英]what's wrong with this function?

    function get_ibo_id() {
    if($registerquery = $this->conn->query("SELECT ibo_id FROM fasttrack WHERE count <    
                   8 && flag = 1 ")){
    $this->increase_count();
    while ($row = $registerquery->fetch_assoc()) { 
           return $row[ibo_id];
        }
    }
    else return "No id";
}

it always enters the if block even if the condition is not satisfied... going crazy 即使不满足条件,它也总是进入if块... 发疯

Well $registerquery will never return false, even if you condition is not met... 好$ registerquery永远不会返回false,即使您的条件不满足...

in if statements you have to get a variable to return true or false... 在if语句中,您必须获取一个变量以返回true或false ...

What I would do is something like this (you will have to adept it to your OOP code): 我要做的是这样的事情(您必须将其熟练应用于OOP代码):

function get_ibo_id() {

$registerquery = $this->conn->query("SELECT ibo_id FROM fasttrack WHERE count < 8 && flag = 1 ");
if (mysql_num_rows($registerquery) > 0) {
$this->increase_count();
    while ($row = $registerquery->fetch_assoc()) { 
           return $row[ibo_id];
        }
    }
    else return "No id";
}

It makes a query,checks if you get more than 0 results back and does what is has to do, otherwise echo's an error... 它进行查询,检查返回的结果是否超过0,并执行必须执行的操作,否则echo是一个错误...

Ladislav 拉吉斯拉夫

I think the problem is that 我认为问题是

$this->conn->query(...)
is not returning FALSE as you might expect. 没有返回您期望的FALSE。
If your query produces an empty result set the mysql_query still returns a resource, not FALSE. 如果查询产生空结果集,则mysql_query仍返回资源,而不是FALSE。 You should check the count of returned rows using 您应该使用以下命令检查返回的行数
 mysql_num_rows($registerquery) mysql_num_rows($ registerquery) 

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

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