Search code examples
phpmysqlarraysjsonarray-push

Adding values to MySQL results array


I'm using this to get MySQL results into an array

$var = array();
$sql = "SELECT doc as document ,part, `desc`, price, qty, total FROM parts LIMIT 10";

$result = mysqli_query($con, $sql);

while($obj = mysqli_fetch_object($result)) {        
$var[] = $obj;  
}

That works great. Is it possible to add a value to each row? Something like

$var = array();
$sql = "SELECT doc as document ,part, `desc`, price, qty, total FROM parts LIMIT 10";

$result = mysqli_query($con, $sql);

while($obj = mysqli_fetch_object($result)) {        
$var[] = $obj;  
array_push($var['url'] = $url);
}

Solution

  • You should append url into $obj and then store $obj into $var array:

    while($obj = mysqli_fetch_object($result)) {  
      $obj->url = $url;     // first store url into obj
      $var[] = $obj;  
    }