我尝试使用此代码通过PHP将MySQL数据显示到编码的HTML表中:
<?php
// Get all the data from the "login" table
$result = mysql_query("SELECT * FROM login") or die(mysql_error());
echo '<table class="table">
<thead>
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>user</th>
</tr>
</thead>
<tbody>';
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
**if( $i % 2 == 0 )** { //line 20
$class = " class='odd'";
}
else {
$class = "";
}
// Print out the contents of each row into a table
echo "<tr" . $class . "><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['user'];
echo "</td><td>";
}
echo "</tbody></table>";
?>
但它向我显示了这个错误:
未定义的变量:我在第20行
第20行错误文本在上面的代码中标记为粗体。
答案 0 :(得分:1)
在$i=0;
循环开始之前添加while
,
并且在$i++
循环结束之前将其递增while
。
$i = 0;
while($row = mysql_fetch_array( $result )) {
//your code
$i++;
}
答案 1 :(得分:1)
试试这个
$i=0;
while($row = mysql_fetch_array( $result )) {
if( $i % 2 == 0 ) {
$class = " class='odd'";
} else {
$class = "";
}
// Print out the contents of each row into a table
echo "<tr" . $class . "><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['user'];
echo "</td><td>";
$i++;
}
答案 2 :(得分:0)
添加
$i = 0;
在您收到错误的while
之前,并在评估中使用$ i ++
答案 3 :(得分:0)
您永远不会递增$i
$i = 0;
while($row = mysql_fetch_array( $result )) {
if($i % 2 == 0)
....
$i++; //You need to increment your $i
}