-1

I am getting this error message " Undefined offset: 0 " . what is the reason and How can it be solved?? here is my code:

<?php            
    $rows= mysqli_query ( $con,"SELECT COUNT(Mobile) AS count  FROM data Where Blood = '$bloodGroup'" );
    $count = mysqli_fetch_assoc( $rows  ) ;
    $count = $count[0]['count']; 
?>

4 Answers4

2

I think mysqli_fetch_assoc return single array without index so you can use like this:

<?php            
    $rows= mysqli_query ( $con,"SELECT COUNT(Mobile) AS count  FROM data Where Blood = '$bloodGroup'" );
    $count = mysqli_fetch_assoc( $rows  ) ;
    $count = $count['count']; 
?>
0

mysqli_fetch_assoc() doesn't return a multi-dimensional array. It returns an array containing one row of results.

$count = $count['count'];

Your code would be correct if you used mysqli_fetch_all(MYSQLI_ASSOC) instead of mysqli_fetch_assoc()

Barmar
  • 669,327
  • 51
  • 454
  • 560
0

Try to update:

<?php            
    $rows = mysqli_query ( $con,"SELECT COUNT(Mobile) AS count  FROM data Where Blood = '$bloodGroup'" );
    $count = mysqli_fetch_assoc( $rows  ) ;
    $count = $count["count"];
?>
Chandra Kumar
  • 3,992
  • 1
  • 15
  • 25
-3

//try this

$count = isset($count[0]['count']) ? $count[0]['count'] : 0; 
Parker Dell
  • 374
  • 4
  • 11