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很多时候,很需要注意的一点就是要做好容错机制