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

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题