在php

时间:2019-01-31 13:06:35

标签: php mysql

我正在尝试在连接到MySQL的PHP​​代码中执行事务查询。但不起作用。我该如何解决?

在没有PHP的情况下在MySQL中执行此查询时,

$query2 = "START TRANSACTION; CALL sp_add_servicesoffer($res,1,2); SELECT @id:=max(Services_Offer.SerOff_ID) FROM Services_Offer; UPDATE Services_Offer SET final_price = (SELECT disc_price from Services_Level where Services_Level.level_code= Services_Offer.level_code)*Services_Offer.service_size WHERE Services_Offer.SerOff_ID BETWEEN @id-5 and @id; COMMIT;";
$result2 = mysqli_query($this->conn,$query2);

if($result2){
    return "ok";
}else{
    return "failed";
}

2 个答案:

答案 0 :(得分:0)

您可以使用mysqli_multi_query

 $query2 = "START TRANSACTION; CALL sp_add_servicesoffer($res,1,2); SELECT @id:=max(Services_Offer.SerOff_ID) FROM Services_Offer; UPDATE Services_Offer SET final_price = (SELECT disc_price from Services_Level where Services_Level.level_code= Services_Offer.level_code)*Services_Offer.service_size WHERE Services_Offer.SerOff_ID BETWEEN @id-5 and @id; COMMIT;";  

$result2 = mysqli_multi_query($this->conn,$query2);

 do {
    /* store first result set */
    if ($result = mysqli_store_result($this->conn)) {
        while ($row = mysqli_fetch_row($result)) {
            printf("%s\n", $row[0]);
        }
        mysqli_free_result($result);
    }
    /* print divider */
    if (mysqli_more_results($this->conn)) {
        printf("-----------------\n");
    }
} while (mysqli_next_result($this->conn));

答案 1 :(得分:0)

我解决了这个问题。对我来说有用

    $query2 = "START TRANSACTION; CALL sp_add_servicesoffer($res,1,2); SELECT @id:=max(Services_Offer.SerOff_ID) FROM Services_Offer; UPDATE Services_Offer SET final_price = (SELECT disc_price from Services_Level where Services_Level.level_code= Services_Offer.level_code)*Services_Offer.service_size WHERE Services_Offer.SerOff_ID BETWEEN @id-5 and @id; COMMIT;";  

    mysqli_begin_transaction($this->conn, MYSQLI_TRANS_START_READ_WRITE);

    $result = mysqli_multi_query($this->conn,$query2);

    mysqli_commit($this->conn);

    if($result){
        return "true";
    }else{
        return "false";
    }