Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP echo xyz if rows in loop contain no data

I am trying to echo some text if my loop returns with no data but can't get it do work. I have tried a few things but no luck.

my code:

$result = mysql_send("SELECT * FROM datatable WHERE id='".
    $_SESSION['id']."'ORDER BY id ASC LIMIT 2");

while($row=mysql_fetch_array($result)) {
    $a = 1;
    extract($row);

    echo 'Trans ID: ';
    echo $row['trans_id'];
    echo '<br>';
    echo 'Amount: ';
    echo $row['amount'];
    echo '&nbsp;';
    echo $row['euros'];
    echo '<br>';
    echo '<br>';
}


if ($a = 1) {
    echo 'No History';
} else {
    echo '<a href="#">View history</a>';
};

Can Anyone help me with how to do if statements on a loop`?

like image 489
user342391 Avatar asked Mar 22 '26 11:03

user342391


1 Answers

You have an assignment, which returns the result of the assignment (i.e. 1)

if ($a = 1) {

instead of a comparison, which is what you probably want:

if ($a == 1) {

Therefore, "No history" will always be echoed.

like image 102
Artefacto Avatar answered Mar 25 '26 01:03

Artefacto