$ mysqli-> affected_rows返回0 multi_query

时间:2016-04-15 12:34:58

标签: php mysql rows-affected

我有简单的代码。此查询工作正常。它直接通过sql和php工作(将$mysqli->query更改为$mysqli->multi_query以使其在php中工作)但它$mysqli->affected_rows返回0。实际上插入了行,当我通过heidisql运行它时,它会报告:

  

受影响的行:2找到的行:0警告:0个查询的持续时间:0.000秒

我应该如何统计受影响的行?我做错了什么?

我也试过了rowCount(),并没有真正帮助过。

include ("../connect.php");

$sql = "SELECT balance INTO @b
            FROM managerstock
            WHERE ID = LAST_INSERT_ID();
            INSERT INTO managerstock (`inOut`, quantity, balance)
            VALUES (1, 1, 3);";

if($result = $mysqli->multi_query($sql)){
  echo $mysqli->affected_rows;
}

2 个答案:

答案 0 :(得分:1)

此处CALL user('param1','param2','param3','param4');是一个存储过程,您可以在其中编写任意数量的查询。

您可以使用ROW_COUNT()函数来获取受影响的行数,并使用select语句将其作为输出获取。

<强>程序

CREATE DEFINER=`root`@`localhost` PROCEDURE `user`(IN `p_mode` VARCHAR(100), IN `p_id` INT(3), IN `p_name` VARCHAR(100), IN `p_contact_no` VARCHAR(100))
    NO SQL
begin
    DECLARE
        row_count int(11);

    if p_mode='update' then
        UPDATE `user` SET `name`=p_name, `contact_no`=p_contact_no WHERE `id`=p_id;
        SET row_count = ROW_COUNT();
        select row_affected as row_count;   
    end if;
end

PHP脚本:

<?php
function execute_qry($sql){
    $fire=mysqli_multi_query($this->conn,$sql);
    $array_result= array();
    do{
        $res = array();
        if($result = $this->conn->store_result())
        {
            while($row = mysqli_fetch_assoc($result))
            {
                array_push($res,$row);
            }
            array_push($array_result,$res);
        }
    }while($this->conn->more_results() && $this->conn->next_result());
    return  $array_result;
}
$query  = "CALL user('update','1','hiren','1234');";
$exe_qry=execute_qry($query);
?>

答案 1 :(得分:-1)

首先插入记录然后进行选择查询

 include ("../connect.php");

$sql = "INSERT INTO managerstock (`inOut`, quantity, balance)
            VALUES (1, 1, 3);SELECT balance INTO @b
            FROM managerstock
            WHERE ID = LAST_INSERT_ID();";

    if($result = $mysqli->multi_query($sql)){
      echo $mysqli->affected_rows;
    }