猿问

为何报错呢?Parse error: syntax error, unexpected 'foreach' (T_FOREACH) in line 29

<?php
$mysqli=new mysqli('localhost','root','','tamia');
$mysqli->set_charset('utf8');
if($mysqli->connect_errno){
    die('CONNECT ERROR:'.$mysqli->connect_error);
}
$sql="SELECT id,usernam,age FROM users";
$mysqli_result=$mysqli->query($sql);
if($mysqli_result && $mysqli_result->num_rows>0){
    while($row=$mysqli_result->fetch_assoc()){
        $rows[]=$row;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Document</title>
</head>
<body>
<h2></h2>
<table border="1" cellpadding="0" cellspacing="0" width="80%" bgcolor="#ABCDEF">
    <tr>
        <td>编号</td>
        <td>用户名</td>
        <td>年龄</td>
    </tr>
        <<?php $i=1 foreach($rows as $row):?>
    <tr>
        <td><?php echo $i;?></td>
        <td><?php echo $row['username'];?></td>
        <td><?php echo $row['age'];?></td>
    </tr>
        <?php endforeach?>
</table>
</body>
</html>

 按照老师的些的,提示29行语法错误哇,

eeerrr
浏览 2499回答 1
1回答

eeerrr

<<?php $i=1 foreach($rows as $row):?> 问题如下: 1,行首多了个< 2,$i=1后没有加分号 <?php $i=1; foreach($rows as $row):?>自己找到问题原因了。总结一下,基本上自己都是可以找到原因的,只是在出错的时候可能不大容易找到问题所在——这是一种心理驱使。不妨先把问题放一放,过一会再找,或者等到第二天一看,就全然明白了。
随时随地看视频慕课网APP
我要回答