使用 Require_once() 正确包含数据库连接变量

发布于 2024-09-17 08:18:30 字数 665 浏览 10 评论 0原文

我是一名 php 新手(但是其他语言的长期开发人员),我正在尝试“PHP、MySQL 和 JavaScript”中的一些示例数据库连接。它显示了一个包含数据库连接变量(服务器名、用户名、密码、数据库等)的示例文件。我有一个 php 文件,其中有一些我编写的函数,其中一个有一些 SQL 查询。无论出于何种原因,在该文件中调用 require_once 不会输出任何错误(我有 E_ALL 配置),但我的数据库 php 文件中的这些变量为空。

我用该函数中的所有变量调用了 echo 来查看到底发生了什么,当然它会打印一个空行。世界上有什么是超出范围的?我必须错过一些简单的东西。

这是我正在做的一个示例

db_login.php

<?php
    $db_server = 'localhost';
    // ....
?>

functions.php

<?php
    require_once('db_login.php');

    function myfunction() {
        echo "$db_server";
        // ...
    }
?>

你可以说我疯了,但这不应该足够简单吗?

I'm a php newbie (but long time developer in other languages) and I'm trying some example db connections in "PHP, MySQL, & JavaScript". It shows an example file to include db connection variables (servername, username, password, database, etc.). I have a php file which has a handful of functions I wrote and one of them has a few SQL queries. For whatever reason, calling require_once in that file doesn't output any errors (I have E_ALL config'd) yet those variables in my database php file are null.

I called an echo with all the variables within that function to see what the heck is going on and of course it prints a blank line. What in the world is out of scope? I have to be missing something simple.

Here's an example of what I'm doing

db_login.php

<?php
    $db_server = 'localhost';
    // ....
?>

functions.php

<?php
    require_once('db_login.php');

    function myfunction() {
        echo "$db_server";
        // ...
    }
?>

Call me crazy, but shouldn't this be simple enough to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨落成白 2024-09-24 08:18:30

PHP 没有 函数范围类似于 Javascript,因此您无权访问到functions.php 函数内的db_login.php 中的变量。

有多种方法可以解决这个问题。由于您可能使用服务器名称 global 常量 可能是一个很好的解决方案,因为没有什么可以改变它们。

根据您的情况,您可以这样做:

<?php
    require_once('db_login.php');
      // You have access to $db_server here.
      // Create a constant.
    define("DB_SERVER", $db_server);

    function myfunction() {
          // Using a constant. Note that there is no "$".
        echo DB_SERVER ;
          // Constants are interpreted inside double quotes too
        echo "\nMy constant is DB_SERVER";
        // ...
    }
?>

在您的情况下,将服务器名称作为常量可能是合适的。如果您正在处理想要将其视为真正变量的内容,则可以通过值或引用将变量传递到函数中:

myfunction($variable);

  // by value
function myfunction($pass_variable_to_me_by_value)
{
    echo $pass_variable_to_me_by_value;
    // ...
}

function myfunction(& $pass_variable_to_me_by_reference)
{
    echo $pass_variable_to_me_by_reference;
    // ...
}

请注意,在您的情况中,使用 global 关键字或 $GLOBALS 函数内的数组本质上与通过引用传递相同。,但如果您不是从全局范围传递,它们可能会非常不同(例如在类中或从另一个函数中)。

PHP doesn't have function scope like Javascript, so you do not have access to the variables in db_login.php inside the functions of functions.php.

There are multiple ways of dealing with this. Due to your probable use of the server name global constants would probably be a good solution, since nothing can change them.

In your case you can do:

<?php
    require_once('db_login.php');
      // You have access to $db_server here.
      // Create a constant.
    define("DB_SERVER", $db_server);

    function myfunction() {
          // Using a constant. Note that there is no "$".
        echo DB_SERVER ;
          // Constants are interpreted inside double quotes too
        echo "\nMy constant is DB_SERVER";
        // ...
    }
?>

In your case having the server name as a constant is probably appropriate. If you are dealing with something that you want to treat as a true variable, you can pass the variable into the function by value or by reference:

myfunction($variable);

  // by value
function myfunction($pass_variable_to_me_by_value)
{
    echo $pass_variable_to_me_by_value;
    // ...
}

function myfunction(& $pass_variable_to_me_by_reference)
{
    echo $pass_variable_to_me_by_reference;
    // ...
}

As a note, in your case, using the global keyword or the $GLOBALS array inside a function is essentially the same as passing by reference., but if you are not passing from the global scope they can be very different (in a Class or from another function for example).

梦毁影碎の 2024-09-24 08:18:30

您在 db_login.php 中声明的变量是全局变量。为了在函数中访问它们,您需要使用 $GLOBALS 变量,例如 $GLOBALS['db_server'],或者在函数中使用以下命令将它们声明为全局变量: global 关键字,例如global $db_server

The variables you declare in db_login.php are globals. In order to access them in your function, you need to use the $GLOBALS variable, e.g. $GLOBALS['db_server'], or declare them as global inside your function using the global keyword, e.g. global $db_server.

新一帅帅 2024-09-24 08:18:30

在函数“myfunction”内部,您无权访问这些变量...

查看更多信息:http://php.net/manual/en/language.variables.scope.php

Inside of the function "myfunction" you don't have access to these variable...

See more in: http://php.net/manual/en/language.variables.scope.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文