-7

我正在运行一个 PHP 脚本,并且不断收到如下错误:注意:未定义变量:第 135 行 D:\iac\htdocs\datas\scripts\iAccount_core.inc.php 中的 iAccount_db

但是我已经定义了变量!

代码:

iAccount_core.inc.php

...
require_once("iAccount_config.inc.php");
...
function iAccount_level_update($user){
$result = mysql_query ("SELECT exp FROM $iAccount_table WHERE username='$user'");
    $exp = @mysql_result($result, "exp");
if ($exp > -1 and $exp < 61){$update_lev = 1;}
if ($exp > 60 and $exp < 101){$update_lev = 2;}
if ($exp > 100 and $exp < 6001){$update_lev = 3;}
if ($exp > 600 and $exp < 1001){$update_lev = 4;}
if ($exp > 1000 and $exp < 6001){$update_lev = 5;}
if ($exp > 5999){$update_lev = 6;}
mysql_query("UPDATE $iAccount_table SET level='$update_lev' WHERE username='$user'", $iAccount_db);
}
function iAccount_level_size($user){
$result = mysql_query ("SELECT level FROM $iAccount_table WHERE username='$user'");
    $level = @mysql_result($result, "level");
if ($level = 0 or $level = ""){iAccount_level_update($user);}
if ($level = 1){$size = $iAccount_level_maxsize[1];}
if ($level = 2){$size = $iAccount_level_maxsize[2];}
if ($level = 3){$size = $iAccount_level_maxsize[3];}
if ($level = 4){$size = $iAccount_level_maxsize[4];}
    if ($level = 5){$size = $iAccount_level_maxsize[5];}
    if ($level = 6){$size = $iAccount_level_maxsize[6];}
    $size = $size*1024; // return KB
    return($size);
}
function iAccount_level_addexp($exp, $user){
    $result = mysql_query ("SELECT exp FROM $iAccount_table WHERE username='$user'");
    $expp = @mysql_result($result, "exp");
    $update_exp = $expp+$exp;
    mysql_query("UPDATE $iAccount_table SET exp='$update_exp' WHERE username='$user'", $iAccount_db);
}
function iAccount_dirsize($dirName = '.') {
$dir = dir($dirName);
$size = 0;

while($file = $dir->read()) {
echo "$dirName/$file"." -> ".filesize("$dirName/$file")."n";
if ($file != '.' && $file != '..') {
if (is_dir("$dirName/$file")) {
$size += dirsize($dirName . '/' . $file);
} else {
$size += filesize($dirName . '/' . $file);
}
}
}
$dir->close();
return $size;
}
...
$iAccount_db = mysql_connect($iAccount_sql_server, $iAccount_sql_username, $iAccount_sql_password) or iAccount_die('Unable to connect to database. Please check your iAccount MySQL server, username and password configuration options.');
mysql_select_db($iAccount_sql_database, $iAccount_db) or iAccount_die('Unable to select the database. Please check your iAccount MySQL database configuration option.');

iAccount_config.inc.php

...
$iAccount_sql_server = "localhost";
$iAccount_sql_username = "root";
$iAccount_sql_password = "test";
$iAccount_sql_database = "test";
$iAccount_table = "iAccount";
...
4

1 回答 1

0

好的,现在您发布了一些代码,您$iAccount_db在正确的位置进行定义,但是您试图在funcion 的范围内访问它,而变量是在它之外定义的。

糟糕的解决方案$iAccount_db全局(不推荐)

对此的一种变体是使这些变量为 CONSTANTS,因为实际上它们就是这样。

更好的解决方案:将变量作为参数传递给您的函数:

function iAccount_level_update($user, $iAccount_db){ }

此外,您的查询容易受到 SQL 注入的影响(至少在需要时使用 mysql_real_escape_string),我建议切换到 mysqli 甚至更好的 PDO 并使用查询参数化。

于 2013-06-08T10:18:09.580 回答