2

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 :

weer

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

Mark Vincent Manjac
  • 507
  • 1
  • 6
  • 27
jane
  • 211
  • 9
  • 30

3 Answers3

2

Change the following:

if($count = sqlsrv_num_rows($stmt3) > 0){

to

$count = sqlsrv_num_rows($stmt3);    // getting the record count
   if($count > 0){                   // checking if there is some rows present or not
   while($recentBadge = sqlsrv_fetch_array($stmt3)){
   // your code
   }
}

and one more thing do not put full path of the image in badgeImage column as this will create problem when you move code from local to server. Keep only path of image folder and image name.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
1

replace

if($count = sqlsrv_num_rows($stmt3) > 0){

with

 $count = sqlsrv_num_rows($stmt3); 
 if($count > 0){
Naga
  • 2,190
  • 3
  • 16
  • 21
1

Apparently sqlsrv_num_rows() does not return an expected result which is explained here

Alternatively you can use that fix or you can use the following code:

if(sqlsrv_has_rows($stmt3)){
  while( $row = sqlsrv_fetch_array($stmt3, SQLSRV_FETCH_ASSOC) ) {
    $recentBadge[] = $row;
  }

  if(count($recentBadge) > 3){
    foreach(array_rand($recentBadge, 3) as $key){
      $data[] = $recentBadge[$key];
    }
    $recentBadge = $data;
  }
} else {
  $recentBadge = [];
}

foreach($result as $recentBadge){
  echo $recentBadge['BadgeName'], '<img src="'.$badgerecent['BadgeImage'].'">';
}

This way we're checking if there are found rows instead, is so, store them all in an array and count the size of the array instead.

Community
  • 1
  • 1
Xorifelse
  • 7,878
  • 1
  • 27
  • 38