PHP创建过程

时间:2014-08-27 15:45:14

标签: php stored-procedures mysqli

我尝试通过php mysqli创建一个存储过程。我创建了另一个存储过程,我没有遇到问题。这段代码:

$link = new mysqli("localhost", "root", "", "Question");
$link -> multi_query("DROP PROCEDURE IF EXISTS FacturaTerminarFactura;
    CREATE DEFINER=`root`@`%` PROCEDURE `FacturaTerminarFactura`(IN 
    id_factura INT(40), Total_factura DOUBLE, Total_IVA DOUBLE)
    BEGIN
    UPDATE `tb_factura`
        SET
        `tb_factura`.`intId_factura_resolucion_dian` = (SELECT
            MAX(`tb_resolucion_dian`.`intFactura_Actual`) FROM `tb_resolucion_dian`
            WHERE `tb_resolucion_dian`.`intEstado` = 1 LIMIT 1),
            `tb_factura`.`dblTotal_factura` = Total_factura,
            `tb_factura`.`dblIva` = Total_IVA
        WHERE `tb_factura`.`intid_factura` = id_factura;
   UPDATE `tb_resolucion_dian`
        SET `tb_resolucion_dian`.`intFactura_Actual` = 
            tb_resolucion_dian`.`intFactura_Actual` + 1
        WHERE `tb_resolucion_dian`.`intEstado` = 1;
   SELECT `tb_factura`.`intId_factura_resolucion_dian` AS ID
       FROM `tb_factura`
       WHERE `tb_factura`.`intid_factura` = id_factura;
END;");
$log  = "";
if ($mysqli -> warning_count) {
     if ($result = $link -> query("SHOW WARNINGS;")) {
         $row = $result -> fetch_row();
         $log .= $row[0]."-".$row[1]."-".$row[2]."\n";
         $result -> close();
     }
}

这是错误

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL
server version for the right syntax to use near '`.`intFactura_Actual` 1 WHERE 
`tb_resolucion_dian`.`intEstado` = 1; ' at line 14

我看到'+'是问题,但我不知道如何解决。

谢谢,请原谅我的英语。

2 个答案:

答案 0 :(得分:1)

你有一个拼写错误:

tb_resolucion_dian`.`intFactura_Actual` + 1

你错过了tb_resolucion_dian

之前的反引号

此外,根据mysql docs for UPDATE,您不需要在SET子句中命名表名。给出的例子是UPDATE t1 SET col1 = col1 + 1;你有UPDATE的地方t1 SET t1.col1 = t1.col1 + 1,这也可能导致问题。

UPDATE的替代方法是INSERT INTO ... SELECT

答案 1 :(得分:0)

简单修复试试这个

`intFactura_Actual` + 1

`intFactura_Actual` = `intFactura_Actual` + 1

感谢您的评论我看到我没有正确阅读代码(这里看起来很奇怪) 代码中存在拼写错误,就像其他人说的那样。