我有代码:
while($res = mysql_fetch_array($rst))
if(($res)==TRUE)
echo "$res[2]";
else
echo "<img src='imagini/banner.png'>";
首先回显它在我有结果时显示输出。 当条件为假时,第二个回波不会显示图像。 有什么帮助吗? :) 谢谢。
最佳答案
我会修复一些格式问题以提高可读性
while($res = mysql_fetch_array($rst))
if(($res)==TRUE)
echo "$res[2]";
else
echo "<img src='imagini/banner.png'>";
至此
while(false !== ( $res = mysql_fetch_array($rst) ) ){
if($res[2] == true){ // or isset( $res[2] ) ?
echo "$res[2]";
}else{
echo "<img src='imagini/banner.png'>";
}
}
让我们从顶部开始。 mysql_fetch_array
http://php.net/manual/en/function.mysql-fetch-array.php ~ 返回一个数组或 false。
然后我们根据 false 评估它,并将其值分配给 $res。接下来,$res
周围额外的 ()
是没有意义的,即使在这里检查 true 可能你的问题 意义不大,它永远不会进入循环,因为false,因此在循环内 $res
永远不会为 false。最后添加了正确的缩进和{brackets}
。
实际上您可能打算这样做?
if( mysql_num_rows( $rst ) ){
while(false !== ( $res = mysql_fetch_array($rst) ) ){
echo $res[2];
}
}else{
echo '<img src="imagini/banner.png">';
}
在等效的 PDO 中
if( $stmt->rowCount() ){
while(false !== ( $res = $stmt->fetch(PDO::FETCH_NUM) ) ){
echo $res[2];
}
}else{
echo '<img src="imagini/banner.png">';
}
请注意 mysql_* 系列函数从 PHP5.5 开始已弃用 http://php.net/manual/en/book.pdo.php
关于php - while、if 和 else 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31756432/