dousi2553 2013-06-08 09:09 采纳率: 0%
浏览 50
已采纳

php错误 - 注意:未定义的变量:第135行的D:\ iac \ htdocs \ datas \ scripts \ iAccount_core.inc.php中的iAccount_db [关闭]

I am running a PHP script, and keep getting errors like: Notice: Undefined variable: iAccount_db in D:\iac\htdocs\datas\scripts\iAccount_core.inc.php on line 135

But I have already defined the variable!

code:

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";
...
  • 写回答

1条回答 默认 最新

  • duanmu2013 2013-06-08 10:18
    关注

    Ok, now that you posted some code, you're defining $iAccount_db in the correct place, but you're trying to access it when inside the scope of a funcion , while the variable is defined outside of it.

    Bad solution: make $iAccount_db global (not recommended)

    A variant to this would be to make those variable CONSTANTS, since actually that's what they are.

    Better solution: pass the variable as an argument to your function:

    function iAccount_level_update($user, $iAccount_db){ }
    

    Also, your queries are vulnerable to SQL injection (use mysql_real_escape_string at the very least, when required), I suggest switching to mysqli or even better PDO and use query parametrization.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan