脚本中的PHP MySQL语法错误,但直接输入MySQL时,语句有效

时间:2018-04-25 11:06:16

标签: php mysql

我编写了一个MySQL查询,直接传递给MySQL控制台时工作正常但是当我尝试在php脚本中运行查询时,我收到此错误:

  

您的SQL语法有错误;查看与您的MySQL服务器版本相对应的手册,以便在' CREATE TABLE week_rough_stats附近使用正确的语法(ID INT NOT NULL AUTO_INCREMENT,周INT NOT NULL'在第1行

php代码的部分是:

$sql = "DROP TABLE IF EXISTS week_rough_stats; CREATE TABLE week_rough_stats( id INT NOT NULL AUTO_INCREMENT, week INT NOT NULL, qty INT NOT NULL, PRIMARY KEY(id) ) ENGINE = InnoDB; INSERT INTO week_rough_stats(WEEK, qty) SELECT WEEKOFYEAR(epcr_usage_rough.date) AS r_week, SUM(epcr_usage_rough.qty_epcr) AS r_qty FROM epcr_usage_rough GROUP BY r_week; SELECT YEAR(incidents_attended.dtg) AS q_year, WEEKOFYEAR(incidents_attended.dtg) AS q_week, IF( COUNT(epcr_usage.cad_ref) = 0, ANY_VALUE(week_rough_stats.qty), COUNT(epcr_usage.cad_ref) ) AS finalised_epcr, COUNT(incidents_attended.cad) AS qty_incidents, ( ( IF( COUNT(epcr_usage.cad_ref) = 0, ANY_VALUE(week_rough_stats.qty), COUNT(epcr_usage.cad_ref) ) / COUNT(incidents_attended.cad) ) * 100 ) AS compliance FROM incidents_attended LEFT JOIN epcr_usage ON incidents_attended.cad = epcr_usage.cad_ref LEFT JOIN epcr_usage_rough ON CONCAT_WS( '-', YEAR(incidents_attended.dtg), MONTH(incidents_attended.dtg), DAYOFMONTH(incidents_attended.dtg) ) = DATE(epcr_usage_rough.date) LEFT JOIN week_rough_stats ON WEEKOFYEAR(epcr_usage_rough.date) = week_rough_stats.week GROUP BY q_year ASC, q_week ASC";
    if($conn->query($sql) === TRUE) {    
       $result_hdr = $conn->query($sql);
    }else{    
       echo "Error: " . $sql . "<br>" . $conn->error; exit;
    }
    while($row_hdr = $result_hdr->fetch_assoc()) {  
       $end = getEndDate($row_hdr['q_week'], $row_hdr['q_year']);
       echo "<th>$end</th>";

任何想法都将不胜感激!

0 个答案:

没有答案