0

I have a function in PHP that give me some values from DB

$select = mysql_query("SELECT * FROM sfide WHERE accepted = 0");

if($select){
    if(mysql_fetch_row($select) != 0){
        $sep = "~";
        $return = "";
        $i = 0;
        while($row = mysql_fetch_array($select)){
            if($i > 0){
                $return = $return.",";
            };
            $return = $return.$row['id'].$sep.$row['from_'];
            $i += 1;
        }
        return $return;
    }else{
        return "NO";
    }
}else{
return "E, 000, SELECT, ".mysql_error();        
}

The problem is that if I do like in the code above it returns me nothing, instead, if I duplicate the select variable it works:

$select = mysql_query("SELECT * FROM sfide WHERE accepted = 0");
$num_select = mysql_query("SELECT * FROM sfide WHERE accepted = 0");
if($select){
    if(mysql_fetch_row($num_select) != 0){
        $sep = "~";
        $return = "";
        $i = 0;
        while($row = mysql_fetch_array($select)){
            if($i > 0){
                $return = $return.",";
            };
            $return = $return.$row['id'].$sep.$row['from_'];
            $i += 1;
        }
        return $return;
    }else{
        return "NO";
    }
}else{
return "E, 000, SELECT, ".mysql_error();        
}

Why ?

1
  • 1
    mysql_fetch_row returns first row of the resultset and move internal pointer forward so mysql_fetch_array read the second row (or return false if it does not exists). You may take a look on mysql_num_rows function to see how many rows are in the resultset. Commented Mar 11, 2014 at 22:58

1 Answer 1

1

Instead of

mysql_fetch_row($select) != 0

use

mysql_num_rows($select) != 0

that will return an int for you to compare.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.