使用另一个文件中的变量设置数据库连接 [英] Setting up a db connection using variables from another file

查看:42
本文介绍了使用另一个文件中的变量设置数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 mysqli_connect 语句 $dbc 设置数据库连接.我在我的脚本中使用了以下内容:

I am attempting to set up a db connection for a mysqli_connect statement $dbc. I had it working in my script with the following:

DEFINE ('DB_USER','myName';
DEFINE ('DB_PASSWORD','somePass123';
DEFINE ('DB_HOST','localhost';
DEFINE ('DB_NAME','sitename';


// make the db connection
    $dbc = @mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)
    OR die ('Could not connect to mysql: ' . mysqli_connect_error());

然后我在 SO 上发布了一个关于安全性和最佳实践的问题,并被告知如果连接变量(密码、主机、dbname、用户)存在于单独的数据库配置文件中的其他地方,我应该调用连接变量从那里开始.

I then posted a question on SO about security and best practice and was advised that if the connection variables (password, host, dbname, user) exist elsewhere in a separate database configuration file, that I should call the variables for the connection from there.

这是来自 config/database/php 文件的相关片段

Here is the relevant snippet from the file in config/database/php

$config['default'] = array(
    'benchmark' => TRUE,
    'persistent' => FALSE,
    'connection' => array(
        'type' => 'mysqli',
        'user' => 'myName',
        'pass' => 'somepass123',
        'host' => 'localhost',
        'port' => FALSE,
        'socket' => FALSE,
        'database' => 'sitename',
    ),
    'character_set' => 'utf8',
    'table_prefix' => '',
    'object' => TRUE,
    'cache' => FALSE,
    'escape' => TRUE
);

于是我尝试了这个:

DEFINE ('DB_USER','$config['default']['connection']['user'])';
DEFINE ('DB_PASSWORD',$config['default']['connection']['pass']);
DEFINE ('DB_HOST',$config['default']['connection']['host']);
DEFINE ('DB_NAME',$config['default']['connection']['database']);


// make the db connection
    $dbc = @mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME)
    OR die ('Could not connect to mysql: ' . mysqli_connect_error());

当我尝试加载页面时产生以下错误:未定义变量:配置"

Which produced the following error when I tried to load the page: "Undefined variable: config"

然后我尝试了这个:

 $dbc = @mysqli_connect(
    $config['default']['connection']['host'],
    $config['default']['connection']['user'],
    $config['default']['connection']['pass'],
    $config['default']['connection']['database'])

    OR die ('Could not connect to mysql: ' . mysqli_connect_error());

// Set the encoding
mysqli_set_charset($dbc, 'utf8');

// set the query variable
$query = "SELECT MAX(rating_date) 
          AS last_date
          FROM rating
          WHERE incident_id = $incident_id;";

//connect and run the query
$result = mysqli_query($dbc, $query);

 echo $result->fetch_object()->last_date;

?>

然后产生以下错误:致命错误:在...中的非对象上调用成员函数 fetch_object()"

Which then produced the following error: "Fatal error: Call to a member function fetch_object() on a non-object in..."

我不是 100% 确定这告诉我什么.昨天我了解到不能直接在 php 中回显 SQL 查询,因为它是一个对象,所以我剪切 n 粘贴了echo $result->fetch_object()->last_date;"部分.哪个有效.

I'm not 100% sure what this is telling me. Yesterday I learned that one cannot echo a SQL query in php directly because it's an object so I cut n pasted the part "echo $result->fetch_object()->last_date;" which worked.

现在我似乎试图从配置中提取变量,而不仅仅是在函数中定义它们,我可能由于范围问题而无法连接?

It seems that now that I am trying to draw upon the variables from config, rather than just define them in the function, I am unable to connect perhaps due to a scope issue?

具有 $dbc 连接的文件位于文件夹 themes/myname/views/reports/detail.php

The file with the $dbc connection is in a folder themes/myname/views/reports/detail.php

包含数据库配置细节数组的文件在 application/config/database.php

The file with the database configuration details array is in application/config/database.php

好像是变量作用域的问题?

It looks like an issue of variable scope?

制作 $dbc 变量的最佳实践是什么?当 database.php 存在于与我调用它的文件不同的目录中时,如何从它调用变量?我必须包含()整个文件吗?

What would be the best practice here in making my $dbc variable? How do I call the variables from database.php when it exists in a different directory than the file where I'm calling it? Must I include() the whole file?

推荐答案

sane 怎么样:

$dbc = @mysqli_connect(
  $config['default']['connection']['host'],
  $config['default']['connection']['user'],
  $config['default']['connection']['pass'],
  $config['default']['connection']['database'])
OR die ('Could not connect to mysql: ' . mysqli_connect_error());

使用 define() 没有任何好处,你只是想避免将带有用户名和密码的原始字符串放入 mysqli_connect()调用以防发生错误并且该行在错误消息/堆栈跟踪中发送到客户端.

You don't gain anything by using define(), you're only trying to avoid putting the raw strings with your username and password into the mysqli_connect() call in case an error occurs and that line gets sent to the client in an error message/stack trace.

这篇关于使用另一个文件中的变量设置数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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