Skip to Content

How do I access the database for my Drupal site?

Categories:

I'd like to add PHP to a page on my site that would pull the names of certain members out of the database and format them. Problem is, I have no idea where the database is located, what it's called or what username/pass combination I would need to open a connection in the first place. Can someone provide that information?

Comments

PHP Script & Drupal

The development using PHP script is very user-friendly.Flexible to create the website.The code given by richard is working fine. Anything else?

My Personal <a href="http://www.vitaminaddict.net">Blog </a>

Not required

Richard Eriksson's picture

The great thing about Drupal is that you don't even need to create a connection, know the username, password, or even the database name to do database calls in PHP. You just need to use the db_query function (and related functions). The one thing that's strongly recommended is that you wrap the table name in curly brackets, that way it will work with whatever Drupal-powered site you're working with.

A rudimentary example, which prints out the user ID of a mythical user named "Richard", would be something like as follows:

<?php
 $result = db_query("SELECT uid FROM {users} WHERE name = 'Richard'");
 print db_result($result);
?>

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.