-5

I am trying to create a guestbook that shows comments people have posted through an SQL query. I have successfully connected to the SQL database, but the query isn't showing anything. What is wrong here?

</form>
<h2>Current Posts</h2>


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



if ($numrows > 0) {
echo "$rows ['email']";
while ( $row = mysql_fetch_row($result) ){

    $id = $row ['id'];
    $name = $row['name'];
    $email = $row['email'];
    $message = $row['message'];


    $message = n12br($message);

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

mysql_close();

?>
</body>
</html>
josh
  • 57
  • 1
  • 4

4 Answers4

1

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);

?>
Community
  • 1
  • 1
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
0

You are missing a lot of stuff here.

  1. You are closing the PHP tag but not opening it: <?php
  2. You are never actually executing the query, you are missing a call to $result = mysql_query($sql);
  3. You are using $numRows but never actually setting its value: `$numRows = mysql_num_rows($result);
  4. You are using $row = mysql_fetch_row(...) but latter you are reading the resulting row as an associative array instead of a numeric index. You have to use mysql_fetch_assoc instead.
  5. You are never actually connecting to the DB and selecting a schema, I assume you do that somewhere else, but you close the connection at the end. So make sure you are actually connecting.

It seems like you copied this from somewhere else but didn't do it right.

Alonso Urbano
  • 2,266
  • 1
  • 19
  • 26
0

Things that are wrong

  1. The syntax is not within PHP tags so won't do anything
  2. Using single quote marks around a table name is invalid, you need backticks (or nothing at all)
  3. The $sql statement is never actually run
  4. I assume $numrows is never set, so it will never be over 0
  5. $rows['email'] is not set so it wont echo
  6. $result is never set, so you won't be able to iterate through it's rows
CT14.IT
  • 1,635
  • 1
  • 15
  • 31
-5

Original (incorrect) answer

Because you have LIMIT 0, 30. You are telling MySql to give you 0 results starting at index 30.

Also you are using mysql_fetch_row on a query string not a result set, you need to first actually run the query.

-- And indeed i am wrong with my assumption, here is the right thing you need to do:

";  //What is this?
$sql = "SELECT * FROM 'guest_booklet' LIMIT 0, 30 ";

$result = mysql_query($sql);  //you need to add this
Dallas Caley
  • 5,367
  • 6
  • 40
  • 69