If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 1 of 1
  1. #1
    Junior Member
    Join Date
    Feb 2011
    Member #
    [COLOR=rgb(80, 80, 80)]This tutorial will help you make connection to your database with PHP. Itís very simple process but I know how difficult can be for someone who is only starting to learn PHP. To test this example you should download and install Apache server, MySQL database server and PHP.

    Creating config.php
    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. Weíll make local database connection for a start. Put the code below into config.php file and put it in the root folder of your project.

    $host = "localhost"; //database location
    $user = "bitis"; //database username
    $pass = "kaka"; //database password
    $db_name = "bitis"; //database name
    //database connection
    $link = mysql_connect($host, $user, $pass);
    //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 I like to include it to be sure the data will be in utf8 format. Now we have config.php file created and saved in the root folder of our project.

    Include config.php in application

    Donít be distracted with me calling website an application. I call it because you can use this methods in any application. It doesnít necessarily has to be a website.
    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.
    <?php include 'config.php'; ?>

    Executing a query

    To execute a query youíll have to create table in your database and fill it with some data. Letís say we have table named ĎCustomersí with next fields: Name, Address, Phone. What we want to do is simply to display all data stored in this table.
    $sqlstr = mysql_query("SELECT * FROM customers");
    if (mysql_numrows($sqlstr) != 0) {
    while ($row = mysql_fetch_array($sqlstr)) {
    <p><?= $row['name'] ?></p>
    <p><?= $row['address'] ?></p>
    <p><?= $row['phone'] ?></p>
    What have we done here? First line defines sql string which will get our data. You can get a lot of different data by simply changing sql string. If statement in second line verifies if there are any results returned by sql str. This line is optional but I like to use it so it wonít come to unexpected errors. While loop in 3rd line loops through data record set we received with our sql query. If there is 10 records in the table, while loop would repeat 10 times and output the code between its tags. The code between while tags will be repeated and outputted.
    Thatís it.

    Dhanush is a blogger.

    My favourite Blog


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT -6. The time now is 01:33 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: