数据不会显示,我不确定if语句是否正确。
<?php
require_once ('database.php');
$result = mysql_query("SELECT * FROM courselist WHERE flag");
if ((mysql_num_rows($result) == 'yes'){
echo "<table border='1' align='center' >
<tr>
<td align=center> <b>Name</b></td>
<td align=center><b>price</b></td>
<td align=center><b>day</b></td>
<td align=center><b>slot</b></td></td>";
while($data = mysql_fetch_row($result))
{
echo "<tr>";
echo "<td align=center>$data[0]</td>";
echo "<td align=center>$data[1]</td>";
echo "<td align=center>$data[2]</td>";
echo "<td align=center>$data[3]</td>";
echo "</tr>";
}
echo "</table>";
?>
如果flag为yes,则显示数据行;如果flag为no,则不显示数据行
答案 0 :(得分:0)
SELECT * FROM courselist WHERE flag
我相信这不仅会返回一列,因此我会进行如下更改。另外,我还在数据库上测试了您的WHERE语句。即使我在给定的列中确实有数据,它也不会返回任何内容。您必须将列与某个值进行比较。
SELECT * FROM courselist WHERE <columnName>="yes"
旁边
mysql_num_rows
mysql_num_rows —获取结果中的行数
所以它将不起作用
mysql_query
对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回的语句 结果集,mysql_query()成功返回资源,否则返回FALSE 错误。 资源是一个特殊变量,包含对外部资源的引用。
这也将不起作用,因为您在其中保存的资源实际上并未获取数据。
您需要将其余代码更改为例如:
require_once ('database.php');
$result = mysql_query("SELECT * FROM courselist WHERE flag");
while($data = mysql_fetch_row($result))
{
echo "<table border='1' align='center' >
<tr>
<td align=center> <b>Name</b></td>
<td align=center><b>price</b></td>
<td align=center><b>day</b></td>
<td align=center><b>slot</b></td></td>";
echo "<tr>";
echo "<td align=center>$data[0]</td>";
echo "<td align=center>$data[1]</td>";
echo "<td align=center>$data[2]</td>";
echo "<td align=center>$data[3]</td>";
echo "</tr>";
}
echo "</table>";
在这种情况下,您可能不需要,因为如果没有记录为“ yes”的记录,则将不会提取这些记录,因此$ data不会包含它们。
答案 1 :(得分:0)
为什么不尝试仅选择flag为yes的那些记录。然后,您只需要遍历所有记录。检查以下代码:
$result = mysql_query("SELECT * FROM courselist WHERE flag = 'yes' ");
if (mysql_num_rows($result) > 0) {
echo "<table border='1' align='center' >
<tr>
<td align=center> <b>Name</b></td>
<td align=center><b>price</b></td>
<td align=center><b>day</b></td>
<td align=center><b>slot</b></td>
</tr>";
while ($data = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td align=center>$data[0]</td>";
echo "<td align=center>$data[1]</td>";
echo "<td align=center>$data[2]</td>";
echo "<td align=center>$data[3]</td>";
echo "</tr>";
}
echo "</table>";
}
希望它对您有帮助。并且请注意,不建议使用mysql。因此,您应该将数据库连接功能更改为mysqli或pdo。
答案 2 :(得分:0)
我肯定会使用mysqli_函数集而不是mysql_函数集,以便您的代码可以正常工作,无论使用哪个版本的php到php 7+都可以使用
<?php
$link = mysqli_connect ( 'localhost', 'username", 'password', 'database' );
/* now... check the connection */
if ( mysqli_connect_errno () )
{
echo 'The MySQL(i) Connection call failed: ' . mysqli_connect_error ();
exit ();
}
if ( $result = mysqli_query ( $link, 'SELECT * FROM courselist WHERE flag;' ) )
{
/* (?), do we have any resulting row set(s) */
if ( mysqli_num_rows ( $result ) > 0 )
{
/* we have (1) or more resulting set(s), so do this */
echo "<table border='1' align='center' >
<tr>
<td align='center'>
<b>
Name
</b>
</td>
<td align='center'>
<b>
price
</b>
</td>
<td align='center'>
<b>
day
</b>
</td>
<td align='center'>
<b>
slot
</b>
</td>
</tr>";
/* now... process each resulting row set */
while ( $data = mysqli_fetch_assoc ( $result ) )
{
echo " <tr>
<td align='center'>
$data[0]
</td>
<td align='center'>
$data[1]
</td>
<td align='center'>
$data[2]
</td>
<td align='center'>
$data[3]
</td>
</tr>";
} // end while ( _fetch_assoc () )
echo "</table>";
/* now... free the result set */
mysqli_free_result ( $result );
} // end if ( _num_rows () )
} // end if ( _query () )
/* now... close the connection */
mysqli_close ( $link );
?>