PHP Easy Most Database Connection

by Hiroshi on June 23, 2008

in connection, database, MySQL, php mySQL

Setting up a database connection can be a difficult task if you google through different codes present online. Here is the easy most method to create a dbconnection for PHP and mysql.

/* Online settings
$db = mysql_connect(”localhost”, “root”, “password”);
$siteurl =;
*//* Local settings */
$siteurl =;
$db = mysql_connect(”localhost”, “root”, “”);

Always write code both for Online and Local usage.

Comment the code which is not being used. Also use siteurl variable and store website root path in it. It comes very very handy in times. We have to use many times website name inside the pages.

Now you can save this code in a file named like dbcon.php or whatever you like and include this file in php pages where you want to use dbconnection by this.

<?php Include (”dbcon.php”); ?>

Here you are done. Another very simple code for dbconnection if you like, I am writing it here.

$con=mysql_connect(”localhost”,”root”,")or die(”Unable to connect”);
mysql_select_db(”dbname”,$con)or die(”Unable to open database”);

This easy way to connect from mysql database through PHP will give a message if the connetion is not established.

Related Posts

Previous post:

Next post: