Link to home
Start Free TrialLog in
Avatar of nicbrock
nicbrockFlag for United States of America

asked on

How do you access the php database?

How do you access the php database?
Avatar of PranjalShah
PranjalShah
Flag of United States of America image

Do u want to access database using a php script?Do you have all the credentials to access the database?
Avatar of 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: http://www.php.net/manual/en/refs.database.php

Have installed a program or system that includes PHP along with a specific database?
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.                    
Did any of our answers help you nicbrock? No comments back?
Any response from the asker? We have posted some suggestions it would be nice if you made a comment on them.
ASKER CERTIFIED SOLUTION
Avatar of lenamtl
lenamtl
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial