user1071461
user1071461

Reputation:

mysql query not recieving proper response... or something

alrighty so what i want to do should be clear by the code below, for each row i want to run the code with their, if header is 0, run the first if statement, if it's 1 run the second.

Problem is, it doesn't matter what i set it to, it always runs the second if statement thinking header is always 1, even when it isn't.

to test i added echo (int)$row['header'] to the second if statement, and for every row, it echoed 1.

Anyone have any clue on how i can make it do what i want?

Thanks for the help

my php code:

$result = mysql_query("SELECT * FROM LeftMenu_items");
while ($row = mysql_fetch_assoc($result))
{
    if ((int)$row['header'] = 0)
    {
        // echos value is on or not
        echo "<tr><td class='LeftMenu'><a href='" . $row['url'] . "'><b>" . $row['text'] . "</b></a></td></tr>\n";
    }
    else if ((int)$row['header'] = 1)
    {
        // header
        echo "<tr><td class='LeftMenu'>" . (int)$row['header'] . "<b>" . $row['text'] . "</b></td></tr>\n"; 
    }
    else echo "error displaying url";

My table looks like this:

structure:
id  int 
text    text    
url text    
Header  tinyint 

data:
1   Home    http://drudexflash.com/ 1
2   test1   http://text.com/    0
3   ttt blahz   1
4   nullnull    ass 0

Upvotes: 0

Views: 41

Answers (1)

nachito
nachito

Reputation: 7035

The problem is that you're assigning a variable rather than comparing.

if((int)$row['header'] = 0)

should be

if ((int)$row['header'] == 0)

This isn't an uncommon bug, so what I sometimes do is switch the order of the value and variable, just to make it clear that I'm comparing vs assigning.

Example:

if (0 = (int)$row['header']) // even if you don't catch this as incorrect
                             // php will throw a 'parse error'
if (0 == (int)$row['header']) // correct, no error

Some useful documentation: http://www.php.net/manual/en/language.expressions.php http://www.php.net/manual/en/language.operators.comparison.php

Upvotes: 1

Related Questions