注意:未知:在第0行的“未知"中跳过数字键1 [英] Notice: Unknown: Skipping numeric key 1 in Unknown on line 0

查看:295
本文介绍了注意:未知:在第0行的“未知"中跳过数字键1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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++;
    }
}

哪个输出期望的数字字符串(1、17、2、18 ...),此错误消息(十次,使用键1,键2,键3,依此类推):

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):

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

查找此错误,我唯一能找到的答案就是将数组放入超全局变量会导致这种情况.我不相信我将数组放入其中,$row['Count']是一个字符串,不是吗?我找不到关于此错误的stackoverflow上的任何条目.

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.

是什么导致此错误,我应该怎么做才能解决它? (显示的代码只是我尝试和计划使用数据库进行无级分页的设计.)

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.)

推荐答案

PHP会话存储机制最初是围绕注册"变量构建的,因此$_SESSION中的键必须是可以自己视为变量的名称.是的.

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

这意味着$_SESSION[42]是无效的,因为$42不是有效的变量名,并且由于$foo[42]$foo['42']引用相同的内容,所以$_SESSION['42']也是无效的.

This means that $_SESSION[42] is invalid, because $42 wouldn'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['row_count_' . $x] = $row['Count'];)或将其放入数组中,然后可以在其后循环(例如$_SESSION['row_counts'] = array(); ... $_SESSION['row_counts'][$x] = $row['Count'];)

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'];)

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

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_handler which doesn't have this limitation:

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

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.

这篇关于注意:未知:在第0行的“未知"中跳过数字键1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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