How do you access the php database?

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

Expert Comment

ID: 35751075
Do u want to access database using a php script?Do you have all the credentials to access the database?
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35752019
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: http://www.php.net/manual/en/refs.database.php

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

Expert Comment

by:Mark Brady
ID: 35773545
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.                    
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 20

Expert Comment

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

Expert Comment

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

Accepted Solution

lenamtl earned 2000 total points
ID: 36262448
Your question is not clear...you 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 http://www.wampserver.com

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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 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