PHP Easy Most Database Connection

by on February 24th, 2008

Setting up a database connection can be a difficult task. Search Google and the scripts you will find will amaze you. These are lengthy and boring. Here is the easy most method to create a dbconnection for PHP and mysql.

<?php
/* Online settings
$db = mysql_connect("localhost", "root", "password");
mysql_select_db("dbname",$db);
$siteurl = http://www.techmynd.com/;
*//* Local settings */
$siteurl = http://www.techmynd.com;
$db = mysql_connect("localhost", "root", "");
mysql_select_db("dbname",$db);
?>

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.

<?php
$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.


Share

One Review

  1. Alexander says:

    This is one of the best sites I have ever found. Thanks!!! Very nice and informal. I enjoy being here.