Loading...

Show comment message from the database - PHP tutorial

Show comment message from the database - PHP tutorial
Show comment message from the database - Learn PHP backend programming. Today we will display the comments we uploaded in the previous episode.

--

mmtuts is a YouTube channel that focuses on teaching beginner and advanced courses in various multimedia related skills.
We plan to make tutorials available on programming, video production, animation, graphic design, and on software such as the Adobe Creative Cloud programs.

PHP for beginners is a how to series that teaches the PHP coding language to people who are just starting out learning programming. The course teaches how PHP scripting can be made easy and teaches how to build many apps such as a login system, a comment section, how to upload images, how to create users in a website, and much more. Creating dynamic websites with PHP is easy and should not be seen as otherwise, which is why we want to explain the language in a easy to understand way for beginners.

If you have suggestions on new courses, or specific lessons within existing courses you would like to see, then feel welcome to submit them in the comment section or in a private message. ALL suggestions will be seen, but not all will be replied to since we get quite a few every day.

RRelated Posts

Why I Left My $100,000+ Job at Google

Here’s why I left Google and why I am NOT following my passion! :P Keep in touch on Facebook: https://www.facebook.com/entercsdojo Support me on Patreon: https://www.patreon.com/csdojo My

FizzBuzz: One Simple Interview Question

There are a lot of opinions on how to hire coders, and most of them are terrible. The opinions, that is, not the coders. But a basic filter test to make sure someone can do what they say they can:

CComments

  • Andy McBean
    Hey mmtuts, great video and it's helped me out quite a lot. So I'm trying to comment on various articles. So each comment has to relate to the specific article it's intended for. All articles are in db. My comments table has the articleID as well as comment,id,user. The problem I have is that I only seem to able to output the last comment submitted. Previous comments stay in db but only last one displays. Not sure if I have to do INNER JOIN or something? Any suggestions would be great. Thanks. Andy.
  • Gloria Lois
    while(loop){
    functions
    }
    exit(); # put this to avoid repeat display of data when you refresh page
    }
  • Freddy
    What about using AJAX to allow visitors to post a comment and see a reply without refreshing the page? That way it would begin to function like facebook. And also, since it's updating in real-time, show us how to add a notification icon with a number or some other indicator of a new message!!
  • Juan Carlos Domingo
    Where's the next tutorial video after this video?
  • Eliott Donatien
    I am learning a lot with your videos thanks man !
  • Mark Mitchell - London's Mortgage Agent
    I keep getting "Uncaught Error: Call to a member function query() on null in ...coments.inc.php: 10 Stack trace: #0.... setComments(NULL)#1main thrown in... on line 10 - Any ideas?
  • Kai Qin
    So currently if one were to put a single quote ' in the comment box it will not post. Been looking into prepared statements for this anyone else solve this issue so far though?
  • Jason C
    hello, my comments are dupplicating in the database as well as in the front page..how do I solve that?
  • Reflexez
    How do apply this same ideaology but with blogs?
  • Kai Qin
    anyone notice that the box won't accept apostrophe's? how would one fix this?
  • J. Yung
    which one is the previous episode before this?
  • Rudi Pratama
    this is what i'm looking for. i just don't know the logic for reading comments from database. i wish i can understand your logic mmtuts :).
  • Abhik Shrestha
    Hey, how come I can't call the getComments functon?

    Call to undefined function getComments();
  • Navaarun Bhattacherjee
    I just don't understand one thing here! The 'message' variable ($message = $_POST['message']) was declared inside the 'setComments' function, but how the 'getComments' function got access to it (which was outside of the 'setComments' function) with '$row['message']' & showing the result without any error?
  • Sleeper CELL
    bro how to display the latest comment on top
  • Nu
    Great job. thank you so much sir
  • Mark Anthony Sunico
    how to create post with comment and how to create chat box and search friends please help that?? please send my gmail acc. markanthonysunico20@gmail.com
  • crislen ciocson
    I love your tutorials.. thank you very much.!. you're really great.. God Bless you.. :)
  • Marcuzr1
    How can I refresh the comments without reloading the website?
  • Marcuzr1
    How can I show the newest comment on top of the website?
  • FilmonAz
    Thanks for tutorial, very helpfully :)
  • M
    if
    echo nl2br($row['messagel']);
    doset work, try
    echo nl2br(stripslashes($row['messagel']));
    or
    echo nl2br(stripsclashes($row['messagel']));
  • M
    For everyone with issues about cloned comments after refreshing, do this:

    Go to your comment.inc.php and index.php files and add ob_start(); right after <?php

    so it looks like this in both files:

    <?php
    ob_start();


    then go to comment.inc.php and add 
    header("Location: index.php");
    exit();
    inside the setComments function inside the if-statement right after $result = mysqli_query($conn, $sql);

    so it looks like this:
    function setComments($conn) 
    {
          if(isset($_POST['commentSubmit']))
          {
                ...
                ///code///
                ...

                header("Location: index.php");
                exit();
          }
    }


    thank me later
  • Carly Farren
    it says my getComments() function is undefined but it has been placed exactly like in your video any idea on what to do?
  • Some Techie
    How Am I able to make the newest comments first?
  • NotaBot
    When I upload it to my website host I get this error: mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /srv/disk15/2545954/www/volted.biz.ht/dbh.inc.php on line 3
    Connection failed: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
  • SoccerEFM
    I've been searching for this sooo long, thanks for your awesome tutorials and explanation!!
    it seems like there are a very low ammount of php tutorials in comparison to other programming languages
  • Red Eyez
    It is very good tutorial. But when you press the button, it will be recorded in the database even if you don't type anything. Any suggestions? One more thing, how do you change the user name text color only without affecting the message text style?
  • Badal Naftaada
    Thanks so much teacher i have tell your channel all my friends.. please can you made videos for mvc 5 or phyton please if you can thank you so much a lot
  • TantrumSlayer69
    Source code?
  • JCL Trading Company
    @mmtus - I followed same steps to create a page. In my page the user data from sql table needs to be printed in the page . For some reason, the first row doesn't print. Can you help me with this. I would be grateful to you.

    <?php
    include 'dbh.php';


    $email= $_SESSION['Email'];

    $sql = "SELECT * FROM customerData WHERE Email= '$email' ";

    $result = mysqli_query($conn, $sql);

    $resultCheck = mysqli_num_rows($result);


    $row= mysqli_fetch_assoc($result);



    if($resultCheck >0){

    while($row = mysqli_fetch_assoc($result)){

    $POPathway =$row ['POPathway'];
    $OrderTitle = $row ['OrderTitle'];

    $emailid = $row ['Email'];
    $OrderDate = $row ['OrderDate'];
    $_SESSION['id'] = $row ['id'];
    $ID = $_SESSION['id'];





    echo "



    <div class='col-sm-12'>
    <table class='table table-striped'>

    <tbody>
    <tr >

    <td class ='col-sm-6' ><a href='FileFolder/$POPathway' target='_blank'
    title=$POPathway>
    $POPathway</a>
    </td>
    <td class ='col-sm-4'>Updated on : $Date </td>
    <td class ='col-sm-2'>
    <form action ='includes/deleteFile.inc.php'>
    <button type='submit' name = '$ID' class='btn btn-info'>Delete PO</button>
    </form>

    </td>

    </tr>
    </tbody>
    </table>
    </div>

    ";

    }

    }else {


    echo " <h4 class ='text-center' style= 'color: red;'> You have no Purchase Orders. </h4>";

    }

    ?>
  • robert beauchamp
    I want to thank you for the tutorial, my client wanted to add a comment system last minute to his website and this helped alot. However, now he wants to add a reply option to the comments that are posted. I wanted to know how I will be able to do that since I didnt see that option in the tutorial. thank you again for your help.
  • Tobias Heide
    Hey fyr, måske kunne du lave et CMS SYSTEM :D
  • Daniella Johansson
    Thanks... works great.
  • Sudhakar sudha
    what is the use of -> operator can you explain it simply
  • abubakar saeed
    how to make "show more" comment button... i want to limit the comments to show 10comments until i click "show more" button.
  • Ivica Misura
    How can I show different comments on different pages? If anybody knows please help me!
  • Ankan Bhattacharjee
    Notice: Undefined variable: result in C:\xampp\htdocs\commentsec\comments.inc.php on line 16
    Fatal error: Call to a member function fetch_assoc() on null in C:\xampp\htdocs\commentsec\comments.inc.php on line 16

    thats the error I am getting after i got the typo corrected, right now the getComments function is this

    unction getComments($conn){
    $sql = "SELECT * FROM comments ORDER BY cid DECS";
    $results = $conn->query($sql);
    $row = $result->fetch_assoc();
    echo $row['message'];
    }

    also can you help in regards on how to stop the duplication of entries if and when i refresh the page, I did go through the comments below to check if others were facing the same issue, but was not able to understand how they solved it.

    Thanks !!
  • Brothers by Quad
    Fatal error: Call to a member function fetch_assoc() on null
  • Maxim
    Not sure if your still replying but I'm wondering how to change it from showing the oldest comments at the very top to the newest ones, I've googled a bit and can't seem to find what I'm looking for but I can guess its got to do with the cid number etc just not sure how to write it.
  • Vlado Zornic
    Can someone tell me what this fatal error means??
    Fatal error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\comment_section\comments.inc.php on line 20
  • john martin infante
    sir??
    can i have a question? why does my database comments go beyond the comment-box??
  • Gil Nahum
    Why the select statement of "SELECT * FROM comments" doesn't selects all the database instead of the first row?
  • Christian Labandero
    it wont show the comments below..
  • Prime Designs
    Love your tutorials so much man, I have a question though. How does Youtube make it so when you post a comment you see it pop up on the screen but the page doesn't visibly reload?
  • Classic C
    Any one had this problem? - once the comment as been made if its a long one it spills over the comment box! I know its stated about 3mths ago but he didn't say how he fixed it.
    Any help here would be great. Thanks.
  • Gogles Uciha
    "i'm new in here"
    this kind of comment is not acceptable in here .. what can i do for that ? i mean for a ( ' ) sign
  • Oliver Kirkpatrick
    I cant get it to take anything from the comment database but I can put things in it
  • Classic C
    mmtuts I have followed this to the letter (i think) but no comments shows up when i go my comments page. I read all the comments & cannot find out where I'm going wrong!
    Any help would be great.

    <?php

    function setComments($conn) {
    if (isset($_POST['commentSubmit'])) {
    $_uid = $_POST['uid'];
    $_date = $_POST['date'];
    $_message = $_POST['message'];

    $sql = "INSERT INTO comments (uid, date, message) VALUES ('$_uid', '$_date', '$_message')";
    $result = mysqli_query($conn, $sql);
    }

    }

    function getComments($conn) {
    $sql = "SELECT * FROM comments";
    $result = mysqli_query ($conn, $sql);
    $row = mysqli_fetch_assoc ($result); {
    echo $row['uid'];"<br>";
    echo $row['date']."<br>";
    echo $row['message']."<br><br>";

    }
    };
  • Matt Nilsen
    Hi again

    just another question on this tutorial as i am learning php, in the comments.inc.php file should we always close off our php tags as i noticed its not