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

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

问题描述

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

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

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

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

我希望能够使用在 Drupal 的 settings.php 文件中定义的 MySQL 连接设置,而不是这样做.

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.

我正在尝试,未成功将 settings.php 文件包含到 node.tpl.php 和 page.tpl.php 中.

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

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

然后我试图像这样从 settings.php 访问 $databases 数组:

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

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

$databases 数组具有以下结构:

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

推荐答案

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

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 $database 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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