1

I need to connect to mysql database and select some records in Drupal 7. How can I do it from within Drupal 7.

I tried this code, but it doesn't work:

db_set_active('default');


$sql = mysql_query("SELECT * FROM users");
while($result = mysql_fetch_array($sql)) {
echo $result["uid"];
echo $result["name"];

Any ideas?

1 Answer 1

2

In Drupal 7 you are already connected to the database. You want to use the database abstraction layer. You can use db_select if you are trying to select from the users table. See some examples in the link...

<?php
$result = db_select('users', 'u')
    ->fields('u')
    ->execute()
    ->fetchAssoc();
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.