php 注意:Unknown:在第 0 行的 Unknown 中跳过数字键 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18797251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:13:47  来源:igfitidea点击:

Notice: Unknown: Skipping numeric key 1 in Unknown on line 0

php

提问by James Gould

I have the following code:

我有以下代码:

include 'includes/connect.php';
$sp= "clot";
$selectall = mysqli_prepare($connection, "SELECT Count FROM prices WHERE Category = ? ORDER BY ppu LIMIT 11");
mysqli_stmt_bind_param($selectall, 's', $sp);
mysqli_stmt_execute($selectall);
$resulttotal = mysqli_stmt_get_result($selectall);
$x=1;
while($row = mysqli_fetch_array($resulttotal, MYSQLI_ASSOC)){
$_SESSION[$x] = $row['Count'];
$x++;
}
$y=1;
while(isset($_SESSION[$y])){
    if($y==11){
        $_SESSION['nextstart'] = $_SESSION[$y];
        unset($_SESSION[11]);
    }
    else{
        echo($y);
        echo("<br>");
        echo($_SESSION[$y]);
        echo("<br>");
        $y++;
    }
}

Which outputs the expected string of numbers (1, 17, 2, 18...) this error message(ten times, with key 1, key 2, key 3, and so on):

它输出预期的数字字符串 (1, 17, 2, 18...) 此错误消息(十次,使用键 1、键 2、键 3 等):

Notice: Unknown: Skipping numeric key 1 in Unknown on line 0

Looking this error up, the only answer I could find was that putting an array into a superglobal would cause this. I don't believe I've put an array in, $row['Count']is a string, isn't it? I couldn't find any entries on stackoverflow about this error.

查看此错误,我能找到的唯一答案是将数组放入超全局变量会导致此错误。我不相信我已经放入了一个数组,$row['Count']是一个字符串,不是吗?我在 stackoverflow 上找不到关于此错误的任何条目。

What causes this error, and what should I do to fix it? (The shown code is just me experimenting and planning a design for endless pagination using my database.)

是什么导致了这个错误,我应该怎么做才能修复它?(显示的代码只是我使用我的数据库试验和规划无限分页的设计。)

回答by IMSoP

The PHP session storage mechanism was originally built around "registering" variables, so the keys in $_SESSIONmust be names that could be treated as variables in their own right.

PHP 会话存储机制最初是围绕“注册”变量构建的,因此 in 中的键$_SESSION必须是本身可以被视为变量的名称。

This means that $_SESSION[42]is invalid, because $42wouldn't be a valid variable name, and since $foo[42]and $foo['42']refer to the same thing, $_SESSION['42']is invalid as well.

这意味着它$_SESSION[42]是无效的,因为它$42不是一个有效的变量名,并且因为$foo[42]$foo['42']引用相同的东西,所以$_SESSION['42']也是无效的。

The solution is either to use a prefix on your session variables (e.g. $_SESSION['row_count_' . $x] = $row['Count'];) or make them into an array, which you can then loop over etc later (e.g. $_SESSION['row_counts'] = array(); ... $_SESSION['row_counts'][$x] = $row['Count'];)

解决方案是在您的会话变量上使用前缀(例如$_SESSION['row_count_' . $x] = $row['Count'];)或将它们放入一个数组中,然后您可以稍后循环等(例如$_SESSION['row_counts'] = array(); ... $_SESSION['row_counts'][$x] = $row['Count'];

Note: this limitation is actually part of the "serialization handler" used when writing the session to disk, which is why the errors have no context (they're fired while PHP is shutting down). In very recent versions of PHP there is a setting of session.serialize_handlerwhich doesn't have this limitation:

注意:这个限制实际上是将会话写入磁盘时使用的“序列化处理程序”的一部分,这就是错误没有上下文的原因(它们在 PHP 关闭时被触发)。在最新版本的 PHP 中,有一个session.serialize_handler设置没有这个限制:

php_serialize is available from PHP 5.5.4. php_serialize uses plain serialize/unserialize function internally and does not have limitations that php and php_binary have. Older serialize handlers cannot store numeric index nor string index contains special characters (| and !) in $_SESSION. Use php_serialize to avoid numeric index or special character errors at script shutdown.

php_serialize 从 PHP 5.5.4 开始可用。php_serialize 在内部使用普通的序列化/反序列化函数,并且没有 php 和 php_binary 的限制。较旧的序列化处理程序无法在 $_SESSION 中存储数字索引或字符串索引包含特殊字符(| 和 !)。使用 php_serialize 避免在脚本关闭时出现数字索引或特殊字符错误。