user3572565
user3572565

Reputation: 741

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);
}

Upvotes: 0

Views: 871

Answers (2)

Jayesh Chitroda
Jayesh Chitroda

Reputation: 5039

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;  
}

Upvotes: 2

Ranjit Shinde
Ranjit Shinde

Reputation: 1130

Hope this will help.

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

Upvotes: 0

Related Questions