brownzilla
brownzilla

Reputation: 289

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;
  }
 ?>

Upvotes: 0

Views: 79

Answers (5)

Kausha Mehta
Kausha Mehta

Reputation: 2928

Follow the below points:

1) First check $stmt returns data or not.

2) Replace if ($stmt->num_rows > 1) { with if ($stmt->num_rows > 0) {.

3) Replace

$msg = "<div class="alert alert-danger" role="alert"><strong>Ruff oh!</strong> '".$rows['Message']."' ?></div>";

with

$msg = "<div class='alert alert-danger' role='alert'><strong>Ruff oh!</strong> ".$rows['Message']."</div>";

4) Call the function like <?php echo checkMaintenance() ?>.

Upvotes: 0

makallio85
makallio85

Reputation: 1356

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() ?>

Upvotes: 2

Vegeta
Vegeta

Reputation: 1317

Replace the line :

 if ($stmt->num_rows > 1) {

With this:

 if ($stmt->num_rows > 0) {

also wrong syntax

 $msg = "..............  .$rows['Message']."' ?></div>";

replace with

 $msg = '<div class="alert alert-danger" role="alert"><strong>Ruff oh!</strong>' . $rows['Message']. '</div>';

Upvotes: 1

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

Try this:

function checkMaintenance {
    $db = doDB();
    $sql = "SELECT * FROM Maintenance WHERE Enabled = 1";
    $stmt = $db->query($sql);
    $msg = "";
    //If result set having atleast single records...
    if ($stmt->num_rows > 0) {
      //Loop until records exists...
      while ($rows = $stmt->fetch_assoc()) {
        $msg = "<div class='alert alert-danger' role='alert'><strong>Ruff oh!</strong> " . $rows['Message'] . " ?></div>";
        return $msg;//Return HTML structure...
      }
    } else {
      $msg = "";
      return $msg;//return blank...
    }
  }
 ?>

I make some fixing stuff which is require. This should return HTML structure if MySql result set having atleast single record.

Hope this help you.!

Upvotes: 0

Pupil
Pupil

Reputation: 23948

You need two corrections:

Change:

$msg = "<div class="alert alert-danger" role="alert"><strong>Ruff oh!</strong> '".$rows['Message']."' ?></div>";

To:

$msg = '<div class="alert alert-danger" role="alert"><strong>Ruff oh!</strong>' . $rows['Message']. '</div>';

You are mixing up double quotes here, causing parse errors. You can use single and double quotes interchangeably, but, they should not be mixed up.

They should be closed properly.

And

 if ($stmt->num_rows > 1) {

To

 if ($stmt->num_rows > 0) {

You are checking if result set contains at least 2 rows (> 1), which is incorrect, sometimes, if you have one row, it will not consider it showing wrong results.

So, check if result set has atleast one row. (> 0)

Upvotes: 0

Related Questions