2

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>";

3 Answers 3

8
if($beckton == '1')

Not

 if($beckton = '1')

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

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

Comments

3

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).

2 Comments

your right but on this occasion i only ever want to retrieve the latest result
Then you should still add some sort of sort (perhaps ORDER BY id DESC, or something similar).
1

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

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.