Friday, February 21, 2014

Share It

How to connect mysql database using PHP

 We have seen how to fetch youtube videos using simple javascript in our previous tutorial. Today's tutorial is about connecting our database using PHP. Since PHP is a dynamic scripting language and it uses mysql database in the back end to store the data. That means the php file has to be configured to connect the database. This can be done very easily with a simple php code. This tutorial will help you to connect to the database. The database connection needs four parameters to be passed into two functions. mysql_connect() and mysql_select_db() are the two functions to connect to the database.

mysql_connect() function requires three parameters and this function is used to connect to the database. and mysql_select_db() function is used to select the database.

Here is the simple code that you can simply copy and use to connect your database with php.

<?php
$dbhost                            = "localhost"; //host name (usually localhost)
$dbuser                            = "root"; //your database(mysql) user name
$dbpass                            = "database_password"; //your database (mysql) password
$dbname                            = "database_name"; //database name
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");
mysql_select_db($dbname) or die ("Error selecting the database");
?>
Now replace root with your database username database_password with your database password  database_name with your database username. That's it, you are done.

Please don't hesitate to ask your queries. Thank you
Author: Bhanu Chander Bathini (CEO, Tricks Town.)
Hey friends, I am Bhanu Chander a Web Designer/Developer, Content writer, Freelance SEO analyst and a blogger. I am pursuing my M.Tech in Computer Science and Engg. You can contact me on bhanu4funn@gmail.com for web design solutions at low cost with effective SEO.

0 comments: