Skip to main content

How to connect php to mysql database? Simple html comment box tutorial.

First thing first. You will need a little bit knowledge of web programming. Simple HTML will work. Now here is a step by step procedure on how to connect php to mysql database using local server. Lets create a simple comment box. And I will teach you how to do this on both Mac or windows/Linux.

Here is a list of Applications you need before doing some coding.
  1. A text editor ( I would recommend Sublime text)
  2. A local server software (MAMP for mac , Xampp for windows/linux)
Now that you have downloaded your local server software, install it and start the servers.
If you are on mac follow the screenshot to start up the servers.



Bravo ! Your computer is now a server (local though).
If you are on windows/Linux follow the similar procedure to start your servers. 

Now lets get into some simple coding. Since we are going to make a simple comment box, lets create two files "comment.php"and "comment_post.php" inside htdocs folder.

Note : Your default server directory is htdocs. Server will only host the files inside the htdocs directory. But you can change it in your xammp/mamp settings.

But before writing any codes, lets create a database.




  1. After starting server, open your browser goto 'http://localhost/phpMyAdmin/'
  2. Create a new database name 'comments'
  3. Create a table 'comments' with fileds '2'
  4. Fill up the database table as below and save (leave other fields empty).
      • Field                    Type                Length/values
      • Name                    varchar            100
      • Comment              varchar            100
Your database table looks something like this


Now Lets get into some coding !! 


filename : 'comments.php'




<!DOCTYPE html>
<head>
</head><body>
<h1> HTML comment box using PHP and MYSQL</h1>
<form action="comment_post.php" name="comments" method="post" enctype="multipart/form-data">
name:
<br />
<input type="text" name="name">
<br />
comment: <br />
<textarea name="comment" rows="2" cols="50"></textarea>
<br />
<input type="submit" value="submit !">
</form>
<br /></body></html>
<?php
$user = 'root';
$password = 'root';
$db = 'comments';
$host = 'localhost';
$conn= mysqli_connect( $host, $user, $password, $db);
$sql = "SELECT * FROM comments" ;
$result=mysqli_query($conn,$sql);
    if (!$conn) {
        echo "Unable to connect to DB: " . mysqli_error();
        exit;
    }
    else
  {
      while($row = mysqli_fetch_assoc($result))
   {
    $name=$row['name'];
    $comment=$row['comment'];
    echo'<font size="4" color="purple"> <b> '.$name. ' </b>: '.$comment.' </font> <hr></div> ';
       
    }
   }

filename : 'comment_post.php'




<!DOCTYPE html>

<head><meta http-equiv="refresh" content="1; url=comments.php" />
<title></title> </head>
<body>
</body>
</html>


<?php
$name=$_POST['name'];

$comment=$_POST['comment'];
$hostname="localhost";

$username="root";

$password="root";

$db="comments";

$conn=mysqli_connect($hostname,$username,$password,$db);
$sql="INSERT INTO comments(name,comment) VALUES ('$name','$comment') ";
   //default password for mamp is "root" and for xampp is empty

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

if(!query)
{
    echo "could not insert into database <br>";

}

else
{

    echo " Sucessfully inserted inside database";
}

?>

Congratulations , You did it !!


Now goto your browser and hit 'localhost/comments.php' 
Your Comment box using html and php should be working perfectly fine.





Comments

  1. If you get any error on connecting to the database, please leave a comment

    ReplyDelete
    Replies
    1. Yes bro when I do it in android local host it shows error and does not work.
      How to fit it

      Delete
    2. Very nice article!! IntelliMindz is the best IT Training in Bangalore with placement, offering 200 and more software courses with 100% Placement
      Assistance.

      Java Training In Bangalore
      LoadRunner Training In Bangalore
      TestComplete Training In Bangalore
      SDET Training In Bangalore

      Delete
  2. Very good explanation, Thanks for sharing.

    ReplyDelete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. Hi!
    This is a very interesting tutorial. But how can se do to display comment into boxes (bubbles for example).
    Thanks

    ReplyDelete
  5. hi....i have created this comment box...but i want to display comments on page....how can i do it??

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. This data is extremely helpful. Continue sharing these sort of valuable and enlightening article refreshes.
    MBA Talks | Article Submission sites

    ReplyDelete
  9. It is a wonderful data you offered to us I really enjoy by reading your article.
    php course
    php courses in chennai

    ReplyDelete
  10. Hi, Excellent Content, your blog is very useful and also interesting to read. Keep sharing this type of information.
    spring hibernate training in chennai
    java training in chennai velachery

    ReplyDelete
  11. This comment has been removed by a blog administrator.

    ReplyDelete
  12. Thank you for sharing this article. It has a lot of valuable information.

    https://espirittech.com/php-development/

    ReplyDelete

Post a Comment

Popular posts from this blog

How to read nth line of a text file in C programming using file handling?

C program to read the nth line of a file.  This file handling C program illustrates how to read the nth content of a text file.  Suppose your text filename is "documents.txt" and you want to read only 9th line of that file. Here's a source code to do so. #include <stdio.h> int main () { FILE * file = fopen ( "documents.txt" , "r" ); char line [ 256 ]; int i = 0 ; while ( fgets ( line , sizeof ( line ), file )) { i ++; if ( i == 9 ) { printf ( "%s" , line ); } } fclose ( file ); return 0 ; } Similarly if you want to read only the 1st/2nd/3rd/4th/5th or any other line then just run variable i till your desired line.   For example if i  want to read the 10th line of the same file if ( i == 10 ) { printf ( "%s" , line ); }