【问题标题】:Catchable fata error in PDOStatementPDOStatement 中可捕获的致命错误
【发布时间】:2015-09-29 11:02:05
【问题描述】:

我不确定我做错了什么。我的代码应该从 mysql 中的用户表中读取 user_id,然后在下一个 sql 语句中使用它来读取该销售人员过去一周的销售行,然后生成 PDF。在第 56 行,我收到一个可捕获的致命错误: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,所以名字是两个表之间的唯一参考点。谢谢!

【问题讨论】:

    标签: php mysql pdo pdostatement


    【解决方案1】:

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

    CONCAT(fname,' ',lname) as fullName
    

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

    其他建议

    1. 将整个事务放入 try/catch 中,如果捕获异常则回滚
    2. 使用准备好的语句

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

    【讨论】:

    • 感谢您的反馈。但是,添加 'fullName' 现在将错误移到它下面的行。我真的很难过。
    • 我不知道你的下一行是什么,所以我无能为力。如果您从我这里得到更多帮助,您需要更积极地响应
    • 我无法尝试,因为我不在电脑前。
    • 错误转到下一行是哪一行
    • 它仍然抛出相同的可捕获的致命错误,但现在说它在第 57 行。我现在正在重写并尝试准备好的语句。
    猜你喜欢
    • 2012-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    相关资源
    最近更新 更多