Results 1 to 4 of 4

Thread: How to Connect MySQL database from PHP

  1. #1
    Join Date
    Dec 2008

    How to Connect MySQL database from PHP

    How to connect to your MySQL database from a PHP document ?

  2. #2
    Join Date
    May 2008

    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:

        mysql_connect("", "username", "password") or die(mysql_error()) ;
        mysql_select_db("Database_Name") or die(mysql_error()) ;
    The first variable (shown as 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.

  3. #3
    Join Date
    Oct 2005

    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

    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'password';

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

    $dbname = 'petstore';

    $dbhost is the name of MySQL server. When your webserver is on the same machine with the MySQL server you can use localhost or 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.

  4. #4
    Join Date
    May 2008

    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

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


    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 :

    include 'config.php';
    include 'opendb.php';

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

    include 'closedb.php';

Similar Threads

  1. How to connect PHP to mySQL Database
    By Nathen in forum Tips & Tweaks
    Replies: 2
    Last Post: 17-07-2011, 03:49 AM
  2. Connecting C# with MySQL database
    By Quattro in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 10:13 PM
  3. Need MySQL database directory
    By Osman84 in forum Software Development
    Replies: 4
    Last Post: 27-02-2010, 05:18 AM
  4. Replies: 3
    Last Post: 07-11-2009, 09:36 PM
  5. How to Connect to MySQL database on my website?
    By Zombi in forum Software Development
    Replies: 3
    Last Post: 24-09-2009, 01:23 PM

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Page generated in 1,716,347,887.01476 seconds with 17 queries