开发者

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


if($beckton == '1')

Not

 if($beckton = '1')

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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜