douqianxun8540 2015-07-10 15:50
浏览 35
已采纳

PDOStatement中可捕获的致命错误

I'm not sure what I'm doing wrong. My code should read user_id from users table in mysql then use that in the next sql statement to read a line of sales for that sales person for the past week and then generate into a PDF. On line 56, I'm getting a Catchable fatal error: Object of class PDOStatement could not be convert to string, but as far as I can tell it should be a string. Any help would be greatly appreciated!

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

I was wondering if it has something to do with the MySQL CONCAT()? I don't really know a better way to match the salesperson's info though, because first and last name are separate and the sales reporting comes in without their sales ID on it, so name is the only reference point between the two tables. Thanks!

  • 写回答

1条回答 默认 最新

  • doucang2871 2015-07-10 17:02
    关注

    You should use an alias for that custom field:

    CONCAT(fname,' ',lname) as fullName
    

    then change $result to $result['fullName'].

    Additional suggesstion

    1. Put the whole transaction in a try/catch, roll back if you catch an exception
    2. Use prepared statements

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 描述一下元器件的基本功能,pcba板的基本原理
  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan