if condition in php is not running

Asked

Viewed 59 times

1

I have the following code in a PHP file :

<?php if($debug == 1){ ?>
    <button id="btn-debug" class="btn btn-default"><i class="fa fa-bug"></i></button>
<?php } ?>

The variable debug is called here in another file:

$debug = data_setting_value($dbc, 'debug-status');

The function data_setting_value is contained in another file, this function fetches a table in the database for the field value value, which in this case is 1

function data_setting_value($dbc, $id){

    $q = "SELECT * FROM settings WHERE id = '$id'";
    $r = mysqli_query($dbc, $q);
    $data = mysqli_fetch_assoc($r);

    return $data['value'];
}

The problem is that the button is not showing me on the page, and you should see that the value field in the database is 1

  • And how do files relate to each other? All are included in the same file?

  • Yes, I have several functions among the files, that’s not the problem, what I noticed is that the function is not returning any value

  • can try a print_r($debug) before the if and inspect what content the variable is receiving? another question: this column of the bank value is not a string? if yes, the condition would be if($debug == "1")

  • The point is, for the code to work, the three parts must be running in the same context. The second file should include the third, so that the function data_setting_value exist in your symbol table. The same happens in the first file, which must include the second so that $debug exist in its context. Or the three files being included , in the order of precedence, in a fourth file. You are doing something like this?

  • In the index.php file I have included the setup.php file and navigation.php. In setup.php I have included the file that has the function, and in the navigation file I have the if statement

1 answer

0


It is recommended to use === (three signs of comparison) instead of == (two signs of comparison).

See the explanation here: http://php.net/manual/en/language.operators.comparison.php

I don’t think the problem is in PHP and, yes, in your data.

Make the following changes:

In the file that contains: $debug = data_setting_value($dbc, 'debug-status');

<?php
// ..
// ..
$debug = data_setting_value($dbc, 'debug-status');
print_r($debug);
// ..
// ..
?>

And see what’s returning.

In the function file: data_setting_value

function data_setting_value($dbc, $id){

    $q = "SELECT * FROM settings WHERE id = '$id'";
    $r = mysqli_query($dbc, $q);
    $data = mysqli_fetch_assoc($r);

    print_r($data); // <-- Insira este print_r

    return $data['value'];
}

And see the result of SELECT.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.