• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 320
  • Last Modified:

How do you access the php database?

How do you access the php database?
1 Solution
Do u want to access database using a php script?Do you have all the credentials to access the database?
Dave BaldwinFixer of ProblemsCommented:
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?
Mark BradyPrincipal Data EngineerCommented:
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.                    
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Mark BradyPrincipal Data EngineerCommented:
Did any of our answers help you nicbrock? No comments back?
Mark BradyPrincipal Data EngineerCommented:
Any response from the asker? We have posted some suggestions it would be nice if you made a comment on them.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now