PHP 函数中的未定义变量和包含的脚本 [英] PHP Undefined variable in functions and included scripts

查看:43
本文介绍了PHP 函数中的未定义变量和包含的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于这个问题的内容,但我仍然无法解决它.

I've read a LOT of things about this problem, however I still cannot fix it.

在我的函数文件中,我声明了一个具有如下值的变量:

In my functions file I declare a variable with a value like so:

$px_host = "localhost";

我有一个像这样的数据库查询功能:

And I have a database query function like so:

function dbQuery($database, $reqquery){
if(!$connect = mysql_connect($px_host, $px_dbuser, $px_dbpass)){
    exit("Error - cannot connect to MySQL server - " . mysql_error());
}

if(!$database = mysql_select_db($database)){
    exit("Error - cannot select database - " . mysql_error());
}

if(!$query = mysql_query($reqquery)){
    exit("Error - query error.");
}

return $query;
}

每当我尝试运行该函数时,都会收到未定义变量"错误.我试过将变量设置为全局变量,但它仍然说它是未定义的.它们在同一个文件中,并且变量是在函数之前定义和设置的.

And whenever I try and run the function, I get an 'Undefined Variable' error. I've tried setting the variable to global, however it still says it's undefined. They are in the same file and the variable is defined and set before the function.

(我不擅长解释这个,所以试着解决它)包含 dbQuery() 的文件包含在另一个文件中.在另一个文件中,有一个函数使用 dbQuery() 来获取某些信息.是否有可能因为它最初是从第一个文件运行的,变量在范围之外?

(I'm bad at explaining this so try and work round it) The file which contains dbQuery() is included in another file. In the other file, there is a function which uses dbQuery() to get certain information. Is it possible that because it's being initially run from the first file, the variable is outside the scope?

推荐答案

在您的特定情况下,您应该在函数内声明函数外的所有变量为 global.所以

In your specific case you should declare global within function all variables outside the function. So

function dbQuery($database, $reqquery){
    global $px_host,$px_dbuser,$px_dbpass;
    // rest of function
}

但是您的代码可以改进:您应该在脚本的开头定义常量

But your code can be improved: You should define constants at the beginning of your script

define('DB_HOST','localhost');
define('DB_USER','user');
define('DB_PASS','pass');

并在函数内部使用常量(因此无需声明全局变量,因为host、user 和pass 不是变量而是常量,因此更具逻辑性).

and use constants inside the function (so no need to declare global, and it's more logic because host, user and pass aren't variable but constants).

您应该在流程开始时只连接一次数据库,以便函数 dbQuery 仅执行查询(根据函数名称).

You should connect at database only once at the beginning of your flow so the function dbQuery execute only the query (according with the function name).

编辑答案的完整性:

正如一些用户在其他评论中所说的那样,我邀请您阅读 mysql_connect 的 php 文档 并查看红色提示:

As some users say you in other comments, I invite you to read the php doc for mysql_connect and see the red advise:

不鼓励使用此扩展程序.相反,应使用 MySQLi 或 PDO_MySQL 扩展.另请参阅 MySQL:选择 API 指南和相关常见问题以了解更多信息.此函数的替代方法包括:

Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

我不是在这里说你必须在你的项目中做什么,但阅读文档并遵循提示/建议对于你的项目的成功至关重要.:)

I'm not here for say you what you MUST do in your project, but read the doc and follow the tipps/suggestions is essential for the success of your project. :)

这篇关于PHP 函数中的未定义变量和包含的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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