For循环在PHP脚本中不起作用

时间:2016-11-24 10:15:12

标签: php

我编写了一个PHP脚本,用于从SQL数据库中提取数据以显示在网页上。一切似乎都很好但是当我运行脚本时它会抛出错误:

  

解析错误:语法错误,意外')',期待';'在第21行的db_data.php中

第21行是我的FOR循环,我检查了循环的语法,它似乎是正确的,所以我无法理解为什么它失败了。

$result = mysqli_query($con,"SELECT * igi");

echo "<table border='1'>
<tr>
<th>Ref</th>
<th>Nameame</th>
<th>Location</th>
<th>Email</th>
<th>Issue</th>
<th>Urgency</th>
</tr>";

for($row = mysqli_fetch_array($result)){
    echo "<tr>";
    echo "<td>" . $row['REF'] . "</td>";
    echo "<td>" . $row['NAME'] . "</td>";
    echo "<td>" . $row['LOCATION'] . "</td>";
    echo "<td>" . $row['EMAIL'] . "</td>";
    echo "<td>" . $row['ISSUE'] . "</td>";
    echo "<td>" . $row['URGENCY'] . "</td>";
    echo "</tr>";
}
echo "</table>";

mysqli_close($con);

2 个答案:

答案 0 :(得分:5)

更改为

<?php
//                   Missing FROM here vv
$result = mysqli_query($con,"SELECT * FROM igi");

echo "<table border='1'>
<tr>
<th>Ref</th>
<th>Nameame</th>
<th>Location</th>
<th>Email</th>
<th>Issue</th>
<th>Urgency</th>
</tr>";

while($row = mysqli_fetch_array($result)){
   echo "<tr>";
   echo "<td>" . $row['REF'] . "</td>";
   echo "<td>" . $row['NAME'] . "</td>";
   echo "<td>" . $row['LOCATION'] . "</td>";
   echo "<td>" . $row['EMAIL'] . "</td>";
   echo "<td>" . $row['ISSUE'] . "</td>";
   echo "<td>" . $row['URGENCY'] . "</td>";
   echo "</tr>";
}
echo "</table>";

mysqli_close($con);

答案 1 :(得分:0)

正如Anthony Thompson和RJParick所说,你必须将for更改为while。您遇到的错误归因于for,需要3个这样的参数:

for($i; $i<10; $i++) {//Note the ; used to separate params.
    //do something
}

所以你的循环会像这样开始:

while($row = mysqli_fetch_array($result)){

另一个细节,你的SQL不正确使用SELECT * FROM igi

相关问题