PHP Freelancer

Freelance PHP Developer

Mysql Tutorial – Insert data in to table

Posted on | March 8, 2009 | 2 Comments

Hello Friends,

If you are a learner (new) in PHP / Mysql and don’t know how to insert / add data in to table in Mysql database from php file than Here is the easiest and error-free way to insert / add data in to  table in Mysql database.

<?php
///*** This file will help you to insert data in to tables in MYSQL database using PHP – Rakshit Patel **////

include(“mysql-connection.php”);
mysql_select_db(“testing”); // select database

$sql = “INSERT INTO test_table (name, email, message) VALUES (‘john’, ‘john@gmail.com’, ‘hello’)”;

mysql_query($sql) or die(‘Error, insert query failed’);

include(“mysql-connection-close.php”);
?>

Here first i have added mysql-connection.php file which will make connection with Mysql database. If you dont know how to make connection with Mysql database than click here.

Now i have selected Mysql database (named testing ). if you have not created database than click here.

After selecting database , you need to execute the query as written above to insert record in to table. If you have not created table yet , than click here.

Thats it. Just check the record you have inserted by using Select Query.

At last i have included mysql-connection-close.php file.

<?php ///*** This file will close MYSQL connection using PHP – Rakshit Patel **////

mysql_close($connection); // close mysql connection
?>

This is nothing but it will close connection which we have made earlier. Its not necessary but a sign of Good Programmer.

Cheers !!

      
Plugin by: PHP Freelancer

Comments

2 Responses to “Mysql Tutorial – Insert data in to table”

  1. OIS
    March 10th, 2010 @ 10:17 am

    mysql_close is only usefull if you are finished with the logic and are now running some code which will take a lot of time, like the user downloading a big file with readfile. In which case the mysql_close at the end of the file is useless, and will then produce an error if the db is already closed.

    If there are any signs of a good programmer related to databases, one would probably be he/she is using prepared statements.
    Using mysql_close at the end of your code would be a telltale sign of a newbie.

  2. Derrick
    March 10th, 2010 @ 10:30 am

    You need to use mysqli.

Leave a Reply





Freelance PHP Developer