简体   繁体   English

如何在批量插入查询mysql之后从表中获取总插入行数

[英]How to get total inserted row count from a table after bulk insert query mysql

I am using a bulk insert query using php to insert into a mysql table. 我正在使用批量插入查询使用PHP插入到mysql表中。 Here i want to know the total inserted records from this bulk insert query. 在这里,我想知道来自此批量插入查询的总插入记录。 After completion of the bulk insert i am using below code to get total inserted count. 完成批量插入后,我使用下面的代码来获得总插入数。

//here bulk insert complete.Next i am calling this
$row_cnt = mysql_query("SELECT ROW_COUNT()");
echo "Affected:rows:".$inbound_cnt = mysql_num_rows($row_cnt);

Here i am getting Affected:rows:1.And i want last insert id also from this bulk insert. 在这里,我受到了影响:行:1。我还想从此批量插入中获取最后一个插入ID。

echo "current_id:". $current_id = mysql_insert_id();
I am getting '0' ;

How can i find total inserted count? 如何找到总插入数? Any help would be greatly appreciated. 任何帮助将不胜感激。

// my insert query
 $data4 = "";
    $inbound_csv_cnt = count($csv_array);
    for($i=0;$i<$inbound_csv_cnt;$i++)
    {
   $data4.="('".$csv_array[$i][0]."','".$csv_array[$i][1]."','".$csv_array[$i][2]."'),";
    }
    $data4 = substr($data4,0,-1);
    $ins_sql = "INSERT INTO whv_inbound_response(`whv_id`, `rqv_id`, `whv_type`) values $data4" ;
    $res = mysql_query($ins_sql);

You can get total records before insert with this 在插入之前,您可以获得total records

 SELECT COUNT(id) FROM table_name; // save output in $old

Then Insert your data and again count total records now as $new 然后插入您的数据并再次将总记录计为$new

Now find the difference 现在发现差异

$total_inserted_record = $new-$old;

While using loop, you can also set a counter to count the number of rows, even before insert. 在使用循环时,您还可以设置计数器来计算行数,甚至在插入之前。 Also, to get last insert ID, for mysqli procedural you may use 另外,要获取最后一个插入ID,可以使用mysqli程序

$last_id = mysqli_insert_id($conn_string); 

You already have number of records being insert in $inbound_csv_cnt 您已经在$ inbound_csv_cnt中插入了多个记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM