I have this query :
$q4 = "SELECT TOP 5 b.BadgeName, b.BadgeImage FROM BadgeImageTable AS b
INNER JOIN employee_badge AS e
ON e.badge_id = b.BadgeID
WHERE e.employee_id = 2164
ORDER BY e.earned_on DESC ";
$stmt3=sqlsrv_query($conn,$q4);
if($stmt3==false)
{
echo 'error to retrieve info !! <br/>';
die(print_r(sqlsrv_errors(),TRUE));
}
the query returns this :
Now in my php, i am trying to echo any three random images from the 'BadgeImage' column,if the query gives more than 3 rows of data.Else,if the query gives 2 rows,display only 2 images,if 1 row,display only 1 'badgeImage' and so on.
PHP Code that I tried and which is not giving the correct result :
<div class="badgesize">
<?php
if($count = sqlsrv_num_rows($stmt3) > 0){
while($recentBadge = sqlsrv_fetch_array($stmt3)){
$result[] = $recentBadge;
}
if($count > 3){
foreach(array_rand($result, 3) as $val){
$data[] = $result[$val];
}
$result = $data;
}
foreach($result as $recentBadge){
echo $recentBadge['BadgeName'], '<img src="'.$badgerecent['BadgeImage'].'">';
}
} else {
echo 'no results';
}
?>
</div>
When I run the above PHP it is giving 'no result' inh the UI i.e "it is not running the if statement' and directly going to the else part of the PHP. This shouldnot happen as the query fetches 5 rows of data as shown in above picture.
When I try to echo simply (using below code) without the above condition,I am able to do so without any issue.
<img src="<?php echo "".($recentBadge['BadgeImage']).""; ?>" >
I am using PHP 5.5.38 and sql server 2012