tags:

views:

59

answers:

2
    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

+4  A: 

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

Ladislav
PHP is not a strong typed language (see the type comparison table http://php.net/manual/en/types.comparisons.php for what values evaluate to *true*).
Gumbo
That (counting the records in the result set, or simply check if there is a record that meets the conditions) is probably the right direction. But since `$registerquery` is obviously supposed to be an object it can't be passed to `mysql_num_rows()`. And it doesn't necessarily have to be MySQL.
VolkerK
I know, but that is why I said at the top that he will have to adopt it to his own OOP code, since I do not know what kind of Object we are dealing with...
Ladislav
I just want to be nit-picky ;-) E.g.: the return statement indicates that both num\_rows() and the while loop are superfluous.
VolkerK
A: 

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)

garph0
Again: Probably the right direction but the OP didn't mention mysql_query() and `$registerquery->fetch_assoc()` indicates that `$registerquery` is an object, not a mysql result resource.
VolkerK
True :-)I just wanted to point out the problem with the usage return value.
garph0