Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 21-01-2009
Member
 
Join Date: Dec 2008
Posts: 7
How to Connect MySQL database from PHP

How to connect to your MySQL database from a PHP document ?
Reply With Quote
  #2  
Old 21-01-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: How to Connect MySQL database from PHP

You can open a connection to your MySQL database from PHP using the mysql_connect() function. Here is an example:

Code:
   <?php
    mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ;
    mysql_select_db("Database_Name") or die(mysql_error()) ;
    ?>
The first variable (shown as your.hostaddress.com) is the location of your database. Often this is "localhost". The next two variables are your username and password. This is the login you use for your MySQL database.

In the second line, we choose which database you are connecting too. This is needed because the same MySQL username can be linked to several databases.

Once you have connected to the database, you can run any SQL code to access and interact with the information in the database.
Reply With Quote
  #3  
Old 21-01-2009
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: How to Connect MySQL database from PHP

Opening a connection to MySQL database from PHP is easy. Just use the mysql_connect() function like this

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

$dbname = 'petstore';
mysql_select_db($dbname);
?>

$dbhost is the name of MySQL server. When your webserver is on the same machine with the MySQL server you can use localhost or 127.0.0.1 as the value of $dbhost. The $dbuser and $dbpass are valid MySQL user name and password.

Don't forget to select a database using mysql_select_db() after connecting to mysql. If no database selected your query to select or update a table will not work.
Reply With Quote
  #4  
Old 21-01-2009
Member
 
Join Date: May 2008
Posts: 2,383
Closing the Connection

Closing the Connection

The connection opened in a script will be closed as soon as the execution of the script ends. But it's better if you close it explicitly by calling mysql_close() function. You could also put this function call in a file named closedb.php.

Source code : closedb.phps

<?php
// an example of closedb.php
// it does nothing but closing
// a mysql database connection

mysql_close($conn);
?>

Now that you have put the database configuration, opening and closing routines in separate files your PHP script that uses mysql would look something like this :

<?php
include 'config.php';
include 'opendb.php';

// ... do something like insert or select, etc

include 'closedb.php';
?>
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to Connect MySQL database from PHP"
Thread Thread Starter Forum Replies Last Post
How to connect PHP to mySQL Database Nathen Tips & Tweaks 2 17-07-2011 03:49 AM
Connecting C# with MySQL database Quattro Software Development 5 05-03-2010 10:13 PM
Need MySQL database directory Osman84 Software Development 4 27-02-2010 05:18 AM
Mysql Error : Can't connect to local mysql server through socket ' var lib mysql mysql.sock' 2 roshan45 Software Development 3 07-11-2009 09:36 PM
How to Connect to MySQL database on my website? Zombi Software Development 3 24-09-2009 01:23 PM


All times are GMT +5.5. The time now is 11:53 PM.