Results 1 to 4 of 4

Thread: How to make the Database connection in PHP

  1. #1
    Join Date
    Apr 2009
    Posts
    91

    How to make the Database connection in PHP

    I have a complete setup of PHP4.4 and MySQL database how would I make connection with the PHP to MySQL this connection would be important point of view as would update my database or upload data to my database.

  2. #2
    Join Date
    Mar 2008
    Posts
    227

    Re: How to make the Database connection in PHP

    MySQL is a very popular database and there are lot of tutorials available on the web about using PHP with MySQL.If you want to use a database in your application, you have to make config.php file which will contain basic database data. Here we will declare database path, username, password, database name and create connection string. Put the code below into config.php file and put it in the root folder of your project.

    PHP Code:
    <?php
    $host 
    "localhost"//database location
    $user "User1"//database username
    $pass "Newuser123"//database password
    $db_name "practise_data"//database name

    //database connection
    $link mysql_connect($host$user$pass);
    mysql_select_db($db_name);

    //sets encoding to utf8
    mysql_query("SET NAMES utf8");
    ?>
    First 4 lines are basic database data. 2 lines below is connection string which connects to server and then mysql_select_db selects database. The last line is optional but it will suggest that data will be in utf8 format. Now we have config.php file created and saved in the root folder of our project.

  3. #3
    Join Date
    Mar 2008
    Posts
    192

    Re: How to make the Database connection in PHP

    To connect to the database, I did the following steps:
    I created my own MySQL database on my computer, using the prompt screen of mySQL (you can also do it via phpMyAdmin). I added a database called database1 (the same as the real database) and two tables, Name and Addresses. I also added some data. MySql Database without showing any detailed information about connection setting, But if you want to show the success or error message then write code in following way

    $conn = mysql_connect ($host, $username, $pass);

    if (! $conn) {

    die (”A connection to the Server could not be established!”);

    } else {

    echo “Root login”,$host,” successful.”;

  4. #4
    Join Date
    Dec 2008
    Posts
    161

    Re: How to make the Database connection in PHP

    To include config.php into application (lets say it’s a website) simply put next line on the top of the source code of index.php. That will connect to the mySQL database with the currently defined variables. So, obviously, if $connect does that, what would $connect2 do? This will do the following.
    PHP Code:
    <?php
    $connect2
    =mysql_connect($host2,$user2,$pass2); //Connect to the database.
    ?>

Similar Threads

  1. Replies: 9
    Last Post: 14-05-2012, 06:05 PM
  2. How to make database for xcode
    By Farren in forum Software Development
    Replies: 4
    Last Post: 27-03-2012, 11:17 AM
  3. How to make Database connection in ASP.NET
    By Neil'o in forum Software Development
    Replies: 4
    Last Post: 16-11-2011, 11:06 AM
  4. CAR MAKE & MODEL database
    By karv in forum Software Development
    Replies: 2
    Last Post: 07-10-2011, 02:19 PM
  5. Make PAYPAL payment and save data on database
    By Eshita The Techie in forum Software Development
    Replies: 4
    Last Post: 18-01-2011, 03:52 PM

Tags for this Thread

Bookmarks

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,713,436,355.81798 seconds with 17 queries