You are here: Home » MySql » Datatbase connection in php

Datatbase connection in php

52 Flares 52 Flares ×
<?php
$user="newindia";
$pass="123456";
$database="mydatabase";
$link=mysql_connect("localhost",$user,$pass) or die("Mysql not  connected");
$db=mysql_select_db($database,$link) or die("Database not connected");
?>

While using in localhost leave password blank.

By using Oops concept

First create a class

<?php
class db
{
        function db($hostname='localhost',$username='abittu31_channel',$password='channel123',$database='abittu31_channel')
    {
        $this->hostname=$hostname;
        $this->username=$username;
        $this->password=$password;
       $this->conn=mysql_connect($hostname,$username,$password);
    }
}
?>

We have to create an objcet with $in = new db; to use database connection.
Always first create an object then write mysql query.

About Abhishek Sinha

I am Abhishek and I love to code on PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Scroll To Top
52 Flares Twitter 7 Facebook 4 Google+ 3 StumbleUpon 36 Pin It Share 0 LinkedIn 2 52 Flares ×