Deprecated: Assigning the return value of new by reference is deprecated in /home/content/48/9096548/html/xfunda.com/components/libraries/cmslib/spframework.php on line 76

Deprecated: Assigning the return value of new by reference is deprecated in /home/content/48/9096548/html/xfunda.com/components/libraries/cmslib/spframework.php on line 571

Deprecated: Assigning the return value of new by reference is deprecated in /home/content/48/9096548/html/xfunda.com/components/libraries/cmslib/spframework.php on line 572

Deprecated: Assigning the return value of new by reference is deprecated in /home/content/48/9096548/html/xfunda.com/components/libraries/cmslib/spframework.php on line 580

Deprecated: Function eregi() is deprecated in /home/content/48/9096548/html/xfunda.com/templates/ja_barite/ja_menus/Base.class.php on line 62
How to Connect MySQL with PHP

Excellent Fundamental...!!!

 
  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • green color
  • blue color

How to Connect MySQL with PHP

E-mail Print PDF

How to Connect MySQL with PHP

To design dynamic data-driven website with PHP we need to connect with MySQL. This is an extremely important step to establish a connection to the MySQL database. If your script cannot connect to its database, your queries to the database will fail.

Opening a connection to MySQL database from PHP is easy. This can be done with the “mysql_connect” PHP function which helps us open a connection to a MySQL Server and we need to use it as below:

<?php
$dbhost = 'localhost'; //MySQL Host
$dbuser = 'root'; //MySQL User
$dbpass = 'password'; //MySQL Password
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'TestDb'; //MySQL Database to work on
mysql_select_db($dbname);
?>

This is good practice to Connect MySQL with small scale development. While with a large scale development it’s good to use Class an Object Oriented scenario of PHP development.

Connect MySQL with PHP with a Class

Now we will do the same with a PHP class. The piece of code described below will do it for you.

A pseudo-variable, $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object).

<?php
class connect_db {
    function DB() {
        $this->host = "'localhost'"; //MySQL Host
        $this->db = "SourceDB"; //MySQL Database
        $this->user = "SourceUser"; //MySQL User
        $this->pass = " SourcePass"; //MySQL Password
        $this->link = mysql_connect($this->host, $this->user, $this->pass);
        mysql_select_db($this->db);
        register_shutdown_function(array(&$this, 'close'));
    }
   
    function query($query) {
        $result = mysql_query($query, $this->link);
        return $result;
    }
   
    function close() {
        mysql_close($this->link);
    }
}
?>

Create a new file “connect_db.php” and paste the code to it. To use the above class include “connect_db.php” and create an instance of a class connect_db. To create an instance of a class, a new object must be created and assigned to a variable. An object will always be assigned when creating a new object unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation.

<?php
require_once("connect_db.php");
$DB = new connect_db;
?>

 

Trackback(0)
Comments (0)Add Comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
Last Updated ( Sunday, 22 November 2009 11:47 )  

Articles


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Adobe Gallery


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Community


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

FAQ's


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Resources


Warning: Parameter 1 to modMainMenuHelper::buildXML() expected to be a reference, value given in /home/content/48/9096548/html/xfunda.com/libraries/joomla/cache/handler/callback.php on line 100

Who's Online

We have 44 guests online

Subscribe Feeds

Enter your email address:

Sponsor Adds


 
 

Submit Article

We want to develop such a Community portal which will provide a good resource on latest hot technologies such as .NET, VB.NET, C#.NET, ADO.NET, ASP.NET, PHP, Flash, Photoshop. If you like to write for us, this guideline for you. please send your article, faqs on admin[at]xfunda.com

Adobe Gallery Photoshop Tutorials...

Photoshop Tutorial - Create Colorful Composition in Photoshop Photoshop Tutorial - Create Colorful Composition in Photoshop Photoshop Tutorial - Create Colorful Composition in Photoshop Category - Photoshop >> illustration and Art. ...
 
Photoshop Tutorial - Create a Nice Autumn Scenery in Photoshop Photoshop Tutorial - Create a Nice Autumn Scenery in Photoshop Photoshop Tutorial - Create a Nice Autumn Scenery in Photoshop Category - Photoshop >> illustration and Art. ...
 
Photoshop Tutorial - Create a Beautiful Lonely Fantasy Fairy Photoshop Tutorial - Create a Beautiful Lonely Fantasy Fairy Photoshop Tutorial - Create a Beautiful Lonely Fantasy Fairy Category - Photoshop >> illustration and Art. ...
 
Photoshop Tutorial - How To Create a Futuristic Sci-Fi Scene Photoshop Tutorial - How To Create a Futuristic Sci-Fi Scene Photoshop Tutorial - How To Create a Futuristic Sci-Fi Scene Category - Photoshop >> Photo Effects. ...
 
Photoshop Tutorial - Goddess cadeira photo effect in Photoshop Photoshop Tutorial - Goddess cadeira photo effect in Photoshop Photoshop Tutorial - Goddess cadeira photo effect in Photoshop Category - Photoshop >> Photo Effects. ...