How to create a comment box with PHP?

Discussion in 'PHP' started by uleesgold, Mar 3, 2012.

  1. #1
    I'd want it to look like this.
    comment.jpg

    I found a script to download, which I uploaded to my server on HostGator. It gives this error:

    Warning: mysql_connect() [function.mysql-connect]: Lost connection to MySQL server at 'reading initial communication packet', system error: 111 in /home/myusername/public_html/myurl.com/phpcommentbox/connect.php on line 2
    mysql_error()

    this is what's in connect.php

    <?php
    $connect=mysql_connect('127.0.0.1:3307', 'myusername_comment', 'mypassword') or die('mysql_error()');
    $database=mysql_select_db("comment") or die('mysql_error()');
    ?>
    PHP:
    myusername_comment is the name of a database which was created.

    I know I could use Disqus as a comment box, but I don't think that would provide any SEO benefit. I say this because I checked the source code for a page with disqus comments once, and the comments weren't a part of the HTML. Isn't this what the spiders look for, what's in the HTML?
     
    uleesgold, Mar 3, 2012 IP
  2. QueenZ

    QueenZ Peon

    Messages:
    175
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you're supposted to enter you mysql details in there....
     
    QueenZ, Mar 3, 2012 IP
  3. Rowefx

    Rowefx Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The error returned is saying something is wrong whilst connecting to the databse.
     
    Rowefx, Mar 3, 2012 IP
  4. yho_o

    yho_o Well-Known Member

    Messages:
    354
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    #4
    try:

    <?php
    $connect=mysql_connect('localhost:3307', 'myusername_comment', 'mypassword') or die('mysql_error()');
    $database=mysql_select_db("comment") or die('mysql_error()');
    ?>
    Code (markup):
     
    yho_o, Mar 3, 2012 IP
  5. tacnix

    tacnix Member

    Messages:
    11
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    43
    #5
    I hope your query was solved, but if not then take a look...

    First create database and the user, and give the access to user to connect your database from your "cpanel"(your server).
    And here i've few examples for you... i hope it might helpful to you.

    
    /*
    //Simple connection.
    */
    mysql_connect('localhost', 'myusername', 'mypassword');
    
    /*
    //Example 1
    */
    define("HOST", "localhost");
    define("DATABASE", "mydb");
    define("USERNAME", "myusername");
    define("PASSWORD", "mypassword");
    @mysql_connect (HOST, USERNAME, PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());
    @mysql_select_db (DATABASE) OR die('Could not select the database: ' . mysql_error()); 
    
    /*
    //Example 2
    */
    $connection=mysql_connect('localhost', 'myusername', 'mypassword') or die('mysql_error()');
    $database=mysql_select_db('mydb') or die('mysql_error()');
    
    /*
    //Example 3.1(using "function()").
    */
    function connection($hostname, $username, $password) {
        if (!mysql_connect ($hostname, $username, $password)) {
           die ('Could not connect to MySQL: ' . mysql_error() . "!");
       } else {
           mysql_select_db($database) or die ('Could not connect to MySQL: ' . mysql_error() . "!");
           echo 'Connected to My SQL!';
       }
    }
    //Call your "connection" function.
    connection('localhost','myusername','mypassword');
    
    /*
    //Example 3.2 (using "function()").
    */
    function connection($hostname, $username, $password, $database) {
        if (!mysql_connect ($hostname, $username, $password, $database)) {
           die ('Could not connect to MySQL: ' . mysql_error() . "!");
       } else {
           mysql_select_db($database) or die ('Could not connect to MySQL: ' . mysql_error() . "!");
           echo 'Connected to My SQL! Your Database "'.$database.'" has selected.';
       }
    }
    //Call your "connection" function.
    connection('localhost','myusername','mypassword','mydb');
    
    PHP:
     
    Last edited: Mar 4, 2012
    tacnix, Mar 4, 2012 IP