Ricardo
Ricardo

Reputation: 1672

Confusion with `if` `else` and `while`

I have a code where it should check if the result equals to 8 it need to show something and if not it need to show something else and all of that happens inside of a while loop.

while ($row_fpages2 = mysql_fetch_array($result_fanpage2))
{
    if ( $row_fpages2['client'] != NULL ) {

        //GRAPHS
    $sql = "SELECT likes, date FROM statistics_pages WHERE idnum = '".$idnum."' AND page_name = '".$row_fpages2['page_name']."' ORDER BY `statistics_pages`.`date` DESC LIMIT 8";
    $result2 = mysql_query($sql) or die(mysql_error());

    if ($result2) {
        $data   = array();

        while ($row = mysql_fetch_assoc($result2)) {
            $data[]   = $row["likes"];
        }

    if ($result2 == 8) {
    $c_data = count($data)-1;
    $final = array();
    for ($i = 0; $i < $c_data; $i++) {
    $final[] = getZeroResult($data[$i], $data[$i+1]);
}
    $data_string = join(",", $final);

    $stats = '<img src="http://chart.apis.google.com/chart?chs=240x140&cht=ls&chd=t:0,0|'.$data_string.'&chg=20,20&chls=0.75,-1,-1|6,4,1&chm=o,FF9900,1,-1,7,-1|b,3399CC44,0,1,0"></img>';

    } else {
        $stats = '<img src="images/stats_un.jpg"></img>';
    };



    } else {
        print('MySQL query failed with error: ' . mysql_error());
    }

echo '...';

The problem is that the first output always showing the ( == 8) (even if it is not equals to 8) instead of the else output.

Then if i have 2 or more everything comes above the first one is correct but the first one is still showing the ( == 8).

Any help?

Upvotes: 1

Views: 119

Answers (2)

Abhishek bhutra
Abhishek bhutra

Reputation: 1422

May be you would like to use

if(strlen($result2) == 8){
               ...
         }

instead of

if($result2 == 8){
               ...
         }

Hope that solves your problem.

Upvotes: 0

codaddict
codaddict

Reputation: 455000

You do the following which is incorrect:

$result2 = mysql_query($sql) or die(mysql_error());
...
if ($result2 == 8) {

The return value of mysql_query is a resource not an int. What is that you are trying to do there ?

Upvotes: 4

Related Questions