I'm doing an SQL SELECT to retrieve client data to then generate an email from.
The $param
that is being parsed to the API is an object:
var param = {
delivery_id: "string",
order_id: "string",
}
The order_id is then being used to query a second table:
$param = $_REQUEST['param'];
$param = json_decode($param);
$collname = "orders";
$sql = 'SELECT * FROM '.$collname.' WHERE `id` = '.$param->order_id;
$result = $conn->query($sql);
//$conn is defined externally, and is not the cause of the problem
$aData = array();
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$aData[] = $row;
}
sendEmail($aData);
} else {
echo "Error";
}
mysqli_close($conn);
This first query makes perfect sense, and when the $aData
is being returned to the front end as a string, it looks like expected; an array of objects (one object).
But when I try to access it in the sendEmail function, it can't access the key values. Same when I try to echo json_encode($aData[0]->id)
(doesn't work) instead of just echoing json_encode($aData[0])
(works).
The email is actually sent to the hard-coded BCC-mail, but without any of the values within the array.
function sendEmail(&$aData){
$to = $aData[0]->contact_email;
$subject = "".$aData[0]->id;
$txt = "Something something ".$aData[0]->contact.",
something something.
";
$headers = "From: email@email.com" . "
" .
"BCC: email@email.com";
mail($to,$subject,$txt,$headers);
}
How do I access the object keys in the API?
EDIT: question answered by S.DEV. Since data was returned as associative array and not an object, correct targeting syntax was $aData[0]['id'].