Reputation: 13
I have the code:
while($res = mysql_fetch_array($rst))
if(($res)==TRUE)
echo "$res[2]";
else
echo "<img src='imagini/banner.png'>";
First echo it shows me output when I have results. Second echo doesn`t show the image when if condition is false. Any help, please? :) Thanks.
Upvotes: 0
Views: 225
Reputation: 21661
A few formatting things I would fix that increase the readability
while($res = mysql_fetch_array($rst))
if(($res)==TRUE)
echo "$res[2]";
else
echo "<img src='imagini/banner.png'>";
To this
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'>";
}
}
Lets start at the top. mysql_fetch_array
http://php.net/manual/en/function.mysql-fetch-array.php ~ returns an array or false.
Then we are evaluating it against false, and assigning it's value to $res. Next the extra ()
around $res
is meaningless, even checking true here Likely your issue is meaning less, it will never enter the loop as false, so inside the loop $res
is never false. Last addition of proper indenting and {brackets}
.
In reality you probably intend this?
if( mysql_num_rows( $rst ) ){
while(false !== ( $res = mysql_fetch_array($rst) ) ){
echo $res[2];
}
}else{
echo '<img src="imagini/banner.png">';
}
In the equivalent PDO
if( $stmt->rowCount() ){
while(false !== ( $res = $stmt->fetch(PDO::FETCH_NUM) ) ){
echo $res[2];
}
}else{
echo '<img src="imagini/banner.png">';
}
Pleas note mysql_* family of functions are depreciated as of PHP5.5 http://php.net/manual/en/book.pdo.php
Upvotes: 1
Reputation: 682
Most probably the relative path is at fault here. Make sure that is correctly set.
EDIT
While the path thing might be a problem, it is not the biggest here.
What you most probably want is either:
// this tests if there are zero or more rows in your result
if (mysql_num_rows($rst)==0)
{
echo "<img src='imagini/banner.png'/>";
}
else
{
while ($res=mysql_fetch_array($rst))
{
echo $res[2];
}
}
or
// this tests, for each row, if a column is set or not
while ($res=mysql_fetch_array($rst))
{
// index is the index of the column that can be set or not
if (empty($res[index]))
{
echo "<img src='imagini/banner.png'/>";
}
else
{
echo $res[2];
}
}
Also, you seem to retrieve <img>
tags from the database, since you have an default <img>
tag as an alternative. If $res[2]
has this pattern <img src='some_path'/>
maybe you can modify the content of the database to keep just the relative path (without the tag) and echo the tag in the while loop.
Upvotes: 1
Reputation: 11
Try switching the if and else so you can see if it has something to do with what your outputting or if it is failing to reach the else.
if(($res)==FALSE)
echo "<img src='imagini/banner.png'>";
Upvotes: 0