开发者

How to connect to MySQL from within Drupal

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");
whi开发者_运维问答le($result = mysql_fetch_array($sql)) {
echo $result["uid"];
echo $result["name"];

Any ideas?


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();
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜