Here is my code to encode data in JSON format, but it doesn't work. The result is `[]`. Where is my mistake?

```
<?php
$conn = new mysqli('localhost','root','','project');
$data =array();
if(!empty($_GET['masp'])){
    $masp =$_GET['masp'];
$sql ="SELECT *FROM sanpham WHERE masp='$masp'";
$result = mysqli_query($conn,$sql);
if($result){
while($r = mysqli_fetch_assoc($result)){
    $r['masp'] =$data['masp'];
    $r['loai'] =$data['loai'];
    $r['hangsx']=$data['hangsx'];
    $r['tensp']=$data['tensp'];
    $r['img']=$data['img'];
    $r['gia']=$data['gia'];
    $r['nx']=$data['nx'];
 }
}
}
print json_encode($data);

?>

```