PHP echo xyz if rows in a loop do not contain data

I am trying to repeat some text if my loop returns without data, but cannot make it work. I tried a few things but no luck.

my code is:

$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 someone help me on how to do this if the instructions are in a loop?

+3
source share
4 answers

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

if ($a = 1) {

instead of comparing what you probably want:

if ($a == 1) {

Therefore, "No History" will always be displayed.

+3
source

use mysql_num_rows();it will tell you if you have query results

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

$count = mysql_fetch_array($result);

if($count > 0) {
    while($row=mysql_fetch_array($result)) {
        # your code
    }
}

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

if, assigment, comparison: $a = 1 $a = 1 $a == 1 1

+2

if($a == 1)....

+1

==- comparison, =- this is the purpose.

Change to

if ($a == 1) {
0
source

Source: https://habr.com/ru/post/1750667/


All Articles