duai4512 2018-05-16 10:29
浏览 71

将SQL数组数据解析为php mail()

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'].

  • 写回答

1条回答 默认 最新

  • doushi9376 2018-05-16 11:12
    关注

    You can try as following:

    while($row = $result->fetch_assoc()) {
            //$aData[] = $row;
            sendEmail($row);
    }
    //function sendEmail(&$aData){ 
    function sendEmail($aData){
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法