html标签中使用php变量,提示:undefined index: uid in /var/www//list.php
,list.php的具体代码如下:
<code><?php
require 'redis.php';
for ($i=0; $i <= ($redis->get("userid")) ; $i++)
{
$data[] = $redis->hgetall("user:".$i);
}
/* var_dump($data) 的结果如下:
array (size=3)
0 =>
array (size=0)
empty
1 =>
array (size=4)
'uid' => string '1' (length=1)
'username' => string 'jjj' (length=3)
'password' => string '123' (length=3)
'age' => string '20' (length=2)
2 =>
array (size=4)
'uid' => string '2' (length=1)
'username' => string 'lamp' (length=4)
'password' => string '123' (length=3)
'age' => string '20' (length=2)
*/
?>
<table border="1">
<caption>user list</caption>
<tr>
<th>uid</th>
<th>username</th>
<th>age</th>
</tr>
<?php foreach ($data as $v) {?>
<tr>
<td><?php echo $v['uid'] ?></td>
<td><?php echo $v['username'] ?></td>
<td><?php echo $v['age'] ?></td>
</tr>
<?php } ?>
</table>
</code>html标签中使用php变量,提示:undefined index: uid in /var/www//list.php
,list.php的具体代码如下:
<code><?php
require 'redis.php';
for ($i=0; $i <= ($redis->get("userid")) ; $i++)
{
$data[] = $redis->hgetall("user:".$i);
}
/* var_dump($data) 的结果如下:
array (size=3)
0 =>
array (size=0)
empty
1 =>
array (size=4)
'uid' => string '1' (length=1)
'username' => string 'jjj' (length=3)
'password' => string '123' (length=3)
'age' => string '20' (length=2)
2 =>
array (size=4)
'uid' => string '2' (length=1)
'username' => string 'lamp' (length=4)
'password' => string '123' (length=3)
'age' => string '20' (length=2)
*/
?>
<table border="1">
<caption>user list</caption>
<tr>
<th>uid</th>
<th>username</th>
<th>age</th>
</tr>
<?php foreach ($data as $v) {?>
<tr>
<td><?php echo $v['uid'] ?></td>
<td><?php echo $v['username'] ?></td>
<td><?php echo $v['age'] ?></td>
</tr>
<?php } ?>
</table>
</code>
echo 前 isset 下不就可以了吗
你的数组第第一个子元素为空啊找不到uid
php很多时候,很需要注意的一点就是要做好容错机制
立即学习“PHP免费学习笔记(深入)”;
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号