brownzilla brownzilla - 4 years ago 111
SQL Question

MySQL Function keeps on returning blank page

Can someone please make sure that I'm doing this correctly?

I'm not the best at functions.

function checkMaintenance {
$db = doDB();
$sql = "SELECT * FROM Maintenance WHERE Enabled = 1";
$stmt = $db->query($sql);
$msg = "";
if ($stmt->num_rows > 1) {
while ($rows = $stmt->fetch_assoc()) {
$msg = "<div class="alert alert-danger" role="alert"><strong>Ruff oh!</strong> '".$rows['Message']."' ?></div>";
return $msg;
}
} else {
$msg = "";
return $msg;
}
return $msg;
}
?>

Answer Source

I guess function should look like this:

function checkMaintenance() {
    $db = doDB();
    $sql = "SELECT * FROM Maintenance WHERE Enabled = 1";
    $stmt = $db->query($sql);
    $msg = "";
    if ($stmt->num_rows > 0) {
      while ($rows = $stmt->fetch_assoc()) {
        $msg = "<div class='alert alert-danger' role='alert'><strong>Ruff oh!</strong>" . $rows['Message'] . "</div>";
        return $msg;
      }
    }
    return $msg;
  }

and call it

<?= checkMaintenance() ?> or <?php echo checkMaintenance() ?>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download