How do you access the php database?

Posted on 2011-05-12
Last Modified: 2013-12-13
How do you access the php database?
Question by:nicbrock
    LVL 8

    Expert Comment

    Do u want to access database using a php script?Do you have all the credentials to access the database?
    LVL 82

    Expert Comment

    by:Dave Baldwin
    PHP does not have a specific database but it has code that you can use to access many different databases.  This page has links to all the database drivers in PHP:

    Have installed a program or system that includes PHP along with a specific database?
    LVL 20

    Expert Comment

    by:Mark Brady
    Just ask it nicely and it might let you in!
    Kidding. It depends on what sort of database you want to connect to and if you have the right credentials to access that database. You would need to know the username and password to access the DB then you would need to write/or get hold of a php script that will do the job for you. Here is a quick example of a database connection script in php. This does nothing but connect to the database. Once connected, you then use php to get information from, or insert/change information to the database. Anyway, here is an example.

    <?php // simple database connection
    define("HOST", "localhost"); // the hostname goes here but usually localhost
    define("USER", "your_username_here"); // change this to your username
    define("PASSWORD","your_password_here"); // Enter your password between the " " quotes
    define("DB", "your_database_name_here"); // Enter the name of the database you want to connect to
    mysql_connect(HOST, USER, PASSWORD); // This connects you to mysql
    mysql_select_db(DB); // This connects you with the actual database you selected earlier
    // end of file

    Now if you copied the above from the <?php to the ?> inclusive and saved it as "connect.php" then you can include it in any php script that you needed to get database access for. Another quick example. Here a script that will get an email address from a database with a "users" table.

    <?php // test.php
    include("connect.php"); // the connection script we made earlier
    // Now we are connected to your database of choice.
    $username = "Grant";
    $sql = "SELECT `email` from `users` WHERE `username` = 'Grant' LIMIT 1";
    $result = mysql_query($sql)or die(mysql_error());
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    $email = "{$row['email']}"; // This would get Grant's email address
    echo "Grant's email address is " . $email;


    That's it! Pretty simple. This script doesn't actually do anything useful and you would not run a query like that in a real situation as it will return the first "Grant" it finds in the user table and supply his email address. Not very useful but it answers your question on how to connect to a database.

    I hope that has been of some assistance to you.                    
    LVL 20

    Expert Comment

    by:Mark Brady
    Did any of our answers help you nicbrock? No comments back?
    LVL 20

    Expert Comment

    by:Mark Brady
    Any response from the asker? We have posted some suggestions it would be nice if you made a comment on them.
    LVL 24

    Accepted Solution

    Your question is not should elaborate...

    Most of the time PHP scripts are using MySQL database, so you should find the DB in Mysql directory under data folder.

    Let say locally, you want to install some PHP applications with Mysql database...
    Wampserver include PHP, Apache, Mysql servers and PhpMyadmin tool to access Mysql databases, this also allow you to use several servers versions on the same computer.
    Database is located in wamp/bin/mysql/mysqlversion/data
    and you can also access the DB using PhpMyAdmin
    Wampserver is very good and easy to use

    Let say on the net:
    You will have to contact the hosting company that will give you access to cpanel or similare interface and then you will be able to access MySQL database.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now