benhowdle89
benhowdle89

Reputation: 37504

PHP if statement from mysql results

I'm attempting to read results from a mysql db, determine when the result is a 1 or a 0 and then set a variable to either green.png or red.png:

$result = mysql_query("select * from ping limit 1") or die(mysql_error());
               $row = mysql_fetch_array($result);

                                $beckton = $row['beckton'];
                                if ($beckton = '1'){
                                    $beckton_status = "<img src='images/green.png' width='75px' height='75px' />";
                                }
                                else if ($beckton = '0'){
                                    $beckton_status = "<img src='images/red.png' width='75px' height='75px' />";
                                }

In my table 'ping' there is a column called 'beckton' which will be either a 1 or a 0. however when i load the page, all images are set to green.png, when i can clearly see in the DB that some are 0's.

i'm then just doing this: echo "<td>Beckton</td><td>" . $beckton_status . "</td>";

Upvotes: 2

Views: 5763

Answers (3)

Jan Dragsbaek
Jan Dragsbaek

Reputation: 8101

In your comment, you write that you only need your latest result, in this case, you should use some kind of ORDER BY timestamp=latest in your SQL, to ensure you always get the latest result

Upvotes: 1

qJake
qJake

Reputation: 17139

It's also worth mentioning (although @Jason Benson is 100% correct), that you don't have a loop in your code at all. The code you've written will only return the first result of the database, and nothing more. You'll probably need a while() loop to iterate over all the database results, if your table has more than one entry in it (which I think you mentioned).

Upvotes: 3

Jason Benson
Jason Benson

Reputation: 3399

if($beckton == '1')

Not

 if($beckton = '1')

At a cursory glance it seems to me you are setting the $beckton variable, not comparing it.

Upvotes: 8

Related Questions