PHP/SQL Query Not Showing Results

1) Change

$sql = "SELECT * FROM 'guest_booklet' LIMIT 0, 30 ";

To

$sql = "SELECT * FROM `guest_booklet` LIMIT 0, 30 ";

=> Use Backtick instead of single quotes for enclosing table name.

2) You missed $result = mysql_query($sql);

3) You Missed $numrows = mysql_num_rows($result);.

4) Remove echo "$rows ['email']"; line. It’s suspense from where it comes.

Mysql (Updated Code)

<?php

$sql = "SELECT * FROM `guest_booklet` LIMIT 0, 30 ";
$result = mysql_query($sql);
$numrows = mysql_num_rows($result);

if ($numrows > 0) {
  while ( $row = mysql_fetch_row($result) ){

    $id = $row ['id'];
    $name = $row['name'];
    $email = $row['email'];
    $message = n12br($row['message']);

    echo "<div>".$name." - and email is ".$email." <hr/>.".$message."<div>";
  }
}

mysql_close();
?>

[Note: The mysql_* functions are deprecated, they have been removed from PHP 7, your code will stop working when you upgrade to that version. You should not write new code using them, use mysqli_* or PDO instead.]

Click To Know How can I prevent SQL-injection in PHP?

Mysqli (Updated Code)

<?php

//Connection
$servername = "YOUR-VALUES";
$dbname = "YOUR-VALUES";
$user = "YOUR-VALUES";
$password = "YOUR-VALUES";

$connection = mysqli_connect($servername,$user,$password,$dbname);

if (mysqli_connect_errno()){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql = "SELECT * FROM `guest_booklet` LIMIT 0, 30 ";
$result = mysqli_query($connection,$sql);
$numrows = mysqli_num_rows($result);

if ($numrows > 0) {
  while ( $row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

    $id = $row ['id'];
    $name = $row['name'];
    $email = $row['email'];
    $message = n12br($row['message']);

    echo "<div>".$name." - and email is ".$email." <hr/>.".$message."<div>";
  }
}

mysqli_close($connection);

?>

Leave a Comment