Count How Many Rows Inserted From Last SQL Query

前端 未结 2 1471
一个人的身影
一个人的身影 2021-01-18 02:12

I have this query :

INSERT INTO db1.outbox (DestinationNumber, TextDecoded)
SELECT User.CellPhone, \'$SMSMessage\' as TextDecoded
FROM db2.User
WHERE User.Pu         


        
相关标签:
2条回答
  • 2021-01-18 02:56

    Here are some possibilities:

    » If you have an AUTO_INCREMENT column, you can fetch the row number before and after insert

    » SELECT ROW_COUNT() returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE, DELETE, or INSERT (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));
    
    0 讨论(0)
  • 2021-01-18 03:08

    put this on 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());
    
    ?>
    
    0 讨论(0)
提交回复
热议问题