The number of rows inserted from the last SQL query

I have this query:

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 

it inserts several rows into the Outbox table. but I don’t know how many lines are inserted. how to have the number of rows inserted from this SQL syntax? thanks.

Update I got a '-1' as a result of this command:

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

but I see that 27 rows are inserted on my desk. What have I done wrong?

+6
source share
2 answers

put this in your last statement;

 SELECT ROW_COUNT(); 

UPDATE 1

how about using mysql_affected_rows example

 <?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()); ?> 
+9
source

Here are a few possibilities:

& Rdquo; If you have an AUTO_INCREMENT column, you can select the row number before and after insertion

& Rdquo; SELECT ROW_COUNT() returns the number of rows modified, deleted, or inserted by the last statement if it was UPDATE , DELETE or INSERT ( doc )

& Rdquo; You can use mysqli_affected_rows (since mysql_ functions mysql_ deprecated) to get the number of rows affected 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)); 
+1
source

Source: https://habr.com/ru/post/926414/


All Articles