I need to connect to my database in php file in drupal 7. Each time I move from local server to development server, I have seen setting.php
file which have array of database.
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'my_db',
'username' => 'root',
'password' => '',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
How do I aceess this database array in drupal 7 to use it connecting to database
$con = mysqli_connect('localhost','root','','my_db');
I am looking for drupal way to achieve this.
Thanks for Help
EDIT: My mind was in the wrong programming language. Sorry.
EDIT Again: Removing standard PHP DB query methods in favor of Drupal methods.
According to the document you provided, you do not need to create a database connection within the Drupal environment. You can instead use methods provided by Drupal to utilize the existing database connection to execute your queries.
You have two main methods available to you:
*From the Drupal Documentation, the following SQL:
SELECT n.nid, n.title, n.created FROM node n WHERE n.uid = $uid
ORDER BY n.created DESC LIMIT 0, 10;
Is best executed in Drupal as:
$result = db_query_range('SELECT n.nid, n.title, n.created
FROM {node} n WHERE n.uid = :uid
ORDER BY n.created DESC', 0, 10, array(':uid' => $uid));
Again, it might be best to refer to this document, and use the Drupal method.