pr0cz pr0cz - 27 days ago 9
PHP Question

Insert button after mysqli check fails

So, this is my little line of code - but it simply doesn't work. It gets completly ignored and I don't know why. Can anybody help me?
The button should be displayed if the user_level matches 1 in my db.

<?php

$mysqli = new mysqli('host', 'dbuser', 'psw', 'db');
$result = $mysqli->query("SELECT user_level FROM users WHERE user_level = '1'");
if($result->num_rows == 0) {

} else {

?>
<a href="AdminLayout.php">
<button>Admin</button>

</a>


<?php
}
$mysqli->close();
?>

Answer

I solved it now this way:

    $mysqli = new mysqli('host', 'name', 'psw', 'db');
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    if ($result = $mysqli->query("SELECT user_level FROM users WHERE user_level = 1 ")) {

        $r = mysqli_fetch_array($result);

       }
    $link = 'VignettenUebersichtlayout.php';
    if ($_SESSION['user_level'] == $r['user_level']) {     
        printf(' <a href="' .$link. '">
                    <button style="background-image:url(\'blabla.jpg\');">                  
                    </button>
                    </a> ');
                    }

?>