我不确定自己在做什么错。我的代码应从mysql中的用户表中读取user_id,然后在下一个sql语句中使用该代码来读取该销售人员过去一周的销售记录,然后生成PDF。在第56行上,我遇到了一个Catchable致命错误:无法将类PDOStatement的对象转换为字符串,但据我所知它应该是字符串。任何帮助将不胜感激!

for($i=0;$i<$user_array;$i++) {
$uid = $user_array[$i];
try {
    //Create connection
    $con = new PDO("mysql:host=$servername; dbname=$database", $username, $password);
    $con->exec("SET CHARACTER SET utf8");
}
catch(PDOException $ee) {
    echo $ee->getMessage();
}

$con->beginTransaction();
$query = "SELECT CONCAT(fname,' ',lname) FROM users WHERE user_id = '$uid'";
$result = $con->query($query);

if($result !== false) {

    //This throws catchable fatal error: Object of class PDOStatement could not be converted to string - line 56
    $sql = "select saledate, custname, straddr from dplgalionsales
    WHERE CONCAT(agent_first_name,' ',agent_last_name) = '$result'         //<-- line 56
    and saledate > DATE_SUB(NOW(), INTERVAL 1 WEEK)";

    foreach($res->query($sql) as $row) {
        $mydate = date('m/d/Y');
        $dateadd = date('m/d/Y', strtotime($mydate.' + 3 days'));

        $html_table = '<div>Week Ending: ' .$mydate. '<br>Payroll Issued: ' .$dateadd. '</div><br>';
        $html_table .= '<table border="1" cellspacing="0" cellpadding="2" width="100%"><tr><th>Date</th><th>Customer Name</th><th>Address</th></tr>';

        $html_table .= '<tr><td>' .$row['saledate']. '</td><td>' .$row['custname']. '</td><td>' .$row['straddr']. ' ' .$row['city']. ' ' .$row['state']. ' ' .$row['zip']. '</td></tr>';

        $html_table .= '</table>'; //ends HTML table

    }


}

$mpdf = new mPDF();
$mpdf->SetTitle('DPL Galion Sales');
$mpdf->WriteHTML($html_table);
$mpdf->Output('./reports/'.$uid.'/'.date('m-d-Y').'_'.$uidname.'.pdf','F');
exit;
}


我想知道它是否与MySQL CONCAT()有关?不过,我真的不知道一种更好的方式来匹配销售员的信息,因为名字和姓氏是分开的,并且销售报告不带销售ID,因此名称是两个表之间的唯一参考点。谢谢!

最佳答案

您应该为该自定义字段使用别名:

CONCAT(fname,' ',lname) as fullName


然后将$result更改为$result['fullName']

额外建议


将整个事务放在try / catch中,如果发现异常则回滚
使用准备好的陈述




try {
    for ($i = 0; $i < $user_array; $i++) {
        $uid = $user_array[$i];

        //Create connection
        $con = new PDO("mysql:host=$servername; dbname=$database", $username, $password);
        $con->exec("SET CHARACTER SET utf8");


        $con->beginTransaction();
        $sql = "SELECT CONCAT(fname,' ',lname) as fullName FROM users WHERE user_id = :uid";
        $result = $con->prepare($sql);


        if ($result !== false) {
            $result->bindValue(':uid', $uid);
            $row = $result->fetch(PDO::FETCH_ASSOC);
            //This throws catchable fatal error: Object of class PDOStatement could not be converted to string - line 56
            $sql = "select saledate, custname, straddr from dplgalionsales
                    WHERE CONCAT(agent_first_name,' ',agent_last_name) = :fullname         //<-- line 56
                    and saledate > DATE_SUB(NOW(), INTERVAL 1 WEEK)";
            $result = $con->prepare($sql);
            $result->bindValue(':fullName', $row['fullName']);
            $rows = $result->fetchAll();
            foreach ($rows as $row) {
                $mydate = date('m/d/Y');
                $dateadd = date('m/d/Y', strtotime($mydate . ' + 3 days'));

                $html_table = '<div>Week Ending: ' . $mydate . '<br>Payroll Issued: ' . $dateadd . '</div><br>';
                $html_table .= '<table border="1" cellspacing="0" cellpadding="2" width="100%"><tr><th>Date</th><th>Customer Name</th><th>Address</th></tr>';

                $html_table .= '<tr><td>' . $row['saledate'] . '</td><td>' . $row['custname'] . '</td><td>' . $row['straddr'] . ' ' . $row['city'] . ' ' . $row['state'] . ' ' . $row['zip'] . '</td></tr>';

                $html_table .= '</table>'; //ends HTML table

            }


        }

        $mpdf = new mPDF();
        $mpdf->SetTitle('DPL Galion Sales');
        $mpdf->WriteHTML($html_table);
        $mpdf->Output('./reports/' . $uid . '/' . date('m-d-Y') . '_' . $uidname . '.pdf', 'F');
        exit;
    }
} catch (PDOException $ee) {
    $con->rollBack();
    echo $ee->getMessage();
}

关于php - PDOStatement中可捕获的致命错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31345121/

10-11 21:56