Joann
Joann

Reputation: 1377

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

Upvotes: 0

Views: 97

Answers (2)

garph0
garph0

Reputation: 1708

I think the problem is that

$this->conn->query(...)
is not returning FALSE as you might expect.
If your query produces an empty result set the mysql_query still returns a resource, not FALSE. You should check the count of returned rows using
mysql_num_rows($registerquery)

Upvotes: 0

Ladislav
Ladislav

Reputation: 7283

Well $registerquery will never return false, even if you condition is not met...

in if statements you have to get a variable to return true or false...

What I would do is something like this (you will have to adept it to your OOP code):

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...

Ladislav

Upvotes: 4

Related Questions