我不确定我做错了什么。我的代码应该从 mysql 中的用户表中读取 user_id,然后在下一个 sql 语句中使用它来读取该销售人员过去一周的销售行,然后生成 PDF。在第 56 行,我收到一个可捕获的 fatal error :类 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 中可捕获的 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31345121/