如何访问Drupal 7 $数据库变量? [英] How can I have access to the Drupal 7 $database variable?

查看:81
本文介绍了如何访问Drupal 7 $数据库变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在node.tpl.php和page.tpl.php中编写了一些php / mysql查询。



这些页面用于连接到的用户名和密码MySQL在文件中指定。



而不是这样做,我想使用在Drupal的settings.php文件中定义的MySQL连接设置。 / p>

我正在尝试,将settings.php文件成功添加到node.tpl.php和page.tpl.php中。

 <?php include(sites / default / settings.php)?> 

然后我试图从settings.php访问$ databases数组,如下所示:

  $ databases ['default'] ['default'] ['username'] 
pre>

$数据库数组具有以下结构:

  $数据库= array(
'default'=>
array(
'default'=>
array(
'database'=>'sdnndr',
'username'=>'root',
'password'=>'',
'host'=>'localhost',
'port'=> '',
'driver'=>'mysql',
'prefix'=>'',
),
),
);

我的方法不起作用任何人都可以建议

解决方案

让我反思你做错了的方式。您不会将查询写入模板。决不。因为如果你稍后改变,你会失去你的逻辑。将它们放在他们所属的模块中。你不需要包括设置,它已经包括在内。你不会写MySQL查询,因为它们是不安全的,而是使用 http://api.drupal.org/api/drupal/includes--database--database.inc/group/database/7 ,鼓励(在某些情况下甚至强制)安全性。


I have written some php/mysql queries inside node.tpl.php and page.tpl.php.

The username and password that these pages use to connect to MySQL are specified within the files.

Instead of doing this, I would like to be able to use the MySQL connection settings that are defined in Drupal's settings.php file.

I am trying, unsuccessfully include the settings.php file into node.tpl.php and page.tpl.php.

<?php include("sites/default/settings.php") ?> 

Then I am trying to access the $databases array from settings.php like this:

$databases['default']['default']['username']

The $databases array has this structure:

$databases = array (
                   'default' => 
                        array (
                          'default' => 
                               array (
                                    'database' => 'sdnndr',
                                    'username' => 'root',
                                    'password' => '',
                                    'host' => 'localhost',
                                    'port' => '',
                                    'driver' => 'mysql',
                                    'prefix' => '',
                                ),
                        ),
              );

My approach is not working. Can anyone suggest

解决方案

Let me count the way you are doing this wrong. You do not write queries into templates. Never. Because if you change looks later you would lose your logic. Put them in modules where they belong. You do need not include settings, it's already included. You do not write MySQL queries because they are insecure, instead you use http://api.drupal.org/api/drupal/includes--database--database.inc/group/database/7 which encourages (and in some case even enforces) security.

这篇关于如何访问Drupal 7 $数据库变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆