Retrieve(echo) data

Hey :blush:

<?php
$comment = filter_input(INPUT_GET, 'comment');
if (!empty($comment)){
$host = "";
$dbusername = "";
$dbpassword = "";
$dbname = "";
// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_errno() .') '
. mysqli_connect_error());
}
else{
$sql = "INSERT INTO comment(comment)
values ('$comment')";
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
}
else{
echo "Error: ". $sql ."
". $conn->error;
}
$conn->close();
}
}
else{
echo "Comment should not be empty";
die();
}
?>
         
         
         
         <?php

$result = mysqli_query("SELECT comment FROM comment"); 
?>
     <p><?php echo $GET["comment"]; ?></p>
        </div>
  </div> 

The data submitted from the attached form is stored within my DB. Issue is retrieving that data (echo) so it can display. Where in this code do I change it so the echo works properly?

Thank ya :heart:

Well, a SELECT query is just the query to set up the database to retrieve data from.
It is designed so that you can get just one record or millions from the same table.
Once the SELECT is created, you then need to FETCH the data to be able to use it.
You are missing a FETCH command. Here is a site that explains it in simple terms.
I think you should start there. Also, you should learn how to indent your code which
will make it more readable. Good luck! Select Example

1 Like

@ErnieAlex :blush:

Ive tried the SELECT query and it doesn’t work for me. I don’t know how to connect both SELECT and INSERT. How do I take my code that I posted and connect the dots because I must not be connecting it right

Again, Mekaboo, you did a SELECT, but, that just acquires a LIST of items. You still need to FETCH the items. Did you read the example I linked to? I clearly shows how to SELECT and then FETCH the data.

This is just one select, no fetch. You need to add a fetch. Loosely like this:

$result = mysqli_query("SELECT comment FROM comment");
$row = $result->fetch_assoc())

But, normally, you first check to make sure it actually retrieved data from the query. All of that is in the example link I posted for you to review. You should read that.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service