0

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

2 Answers 2

2

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;  
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks - But I get Fatal error: Cannot use object of type stdClass as array on this line $obj['url'] = 'ggggg';
Yes! Perfect. Thank you very much
@JayeshChitroda: if $obj->url = $url; works instead of $obj['url'] = $url; please update the same in the answer. It can helpful for those who looking the same answer in future, it may possible that they miss your comments and simply check your answer only.
0

Hope this will help.

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

1 Comment

Thanks - But this puts the url outside of the row data - {{"document":"IH767898","part":"EHA320","desc":" 1.4 HH 11\/2","price":"10.31","qty":"1","total":"10.31","corder":"Frank"},"url":"www.test.com"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.