将值添加到MySQL结果数组

时间:2016-05-18 13:07:58

标签: php mysql arrays json array-push

我使用它将MySQL结果导入数组

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

这很有效。是否可以为每行添加一个值?像

这样的东西
$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);
}

2 个答案:

答案 0 :(得分:2)

你应该将url附加到$ obj中,然后将$ obj存储到$ var array:

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

答案 1 :(得分:0)

希望这会有所帮助。

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