8

我有这个查询:

INSERT INTO db1.outbox (DestinationNumber, TextDecoded)
SELECT User.CellPhone, '$SMSMessage' as TextDecoded
FROM db2.User
WHERE User.PurchaseDate BETWEEN 2012-01-01 AND 2012-01-31

它对“发件箱”表进行多行插入。但我不知道插入了多少行。如何从该 SQL 语法中插入行数?谢谢。

更新 我得到'-1'作为这个命令的结果:

$insertedRows = mysql_query("SELECT ROW_COUNT()");
$rowInserted = mysql_fetch_array($insertedRows);
$rowInserted = $rowInserted[0];
echo $rowInserted;

但我看到我的桌子上插入了 27 行。我做错什么了?

4

2 回答 2

12

把它放在你的最后一个陈述中;

SELECT ROW_COUNT();

更新 1

如何使用mysql_affected_rows,例如

<?php

   $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
   if (!$link) 
   {
       die('Could not connect: ' . mysql_error());
   }
   mysql_select_db('mydb');

   /* this should return the correct numbers of deleted records */
   mysql_query('you SQL QUERY HERE');
   printf("Records deleted: %d\n", mysql_affected_rows());

?>
于 2012-09-27T14:34:30.547 回答
2

以下是一些可能性:

» 如果你有一AUTO_INCREMENT列,你可以获取插入前后的行号

»SELECT ROW_COUNT()返回最后一条语句更改、删除或插入的行数(如果它是UPDATEDELETEINSERT( doc )

» You can use mysqli_affected_rows (since mysql_ functions are being deprecated) to get the number of affected rows in a previous MySQL operation (doc)

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

if (!$link) {
    printf("Can't connect to localhost. Error: %s\n", mysqli_connect_error());
    exit();
}

/* Insert rows */
mysqli_query($link, "INSERT INTO myTable VALUES (1)");
printf("Affected rows (INSERT): %d\n", mysqli_affected_rows($link));
于 2012-09-27T15:07:58.673 回答