PHP/mysql get number of affected rows of UPDATE statement

前端 未结 5 1040
一整个雨季
一整个雨季 2020-12-09 19:36

With php/mysql how can i get the number of rows that a query affected?

what i tried so far:

$result = mysql_query($q);
mysql_num_rows($result);


        
相关标签:
5条回答
  • 2020-12-09 20:16

    You also may want to use a ROW_COUNT() function, e.g. -

    UPDATE table1 SET column1 = 100 WHERE column2 = 10;
    SELECT ROW_COUNT();
    

    From the reference - ROW_COUNT() returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE, DELETE, or INSERT...

    0 讨论(0)
  • 2020-12-09 20:20

    Whe can also do it using PDO :

    $db = new PDO('', '', '')// your connection
    $sql = "UPDATE tb_table SET rowname = value WHERE rowid = 1";
    $query = $db->query($sql);
    echo $query;
    
    0 讨论(0)
  • 2020-12-09 20:21

    clamp,

    you need to supply a resource to mysql_affected_rows, not a result record. See the links that the others have posted for additional information.

    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    $result = mysql_query($q);
    echo mysql_affected_rows($link);
    
    0 讨论(0)
  • 2020-12-09 20:31

    if you're using PDO (wich i would recommend), for a direct query exec() returns the number of affected rows. for Prepared Statements theres a method called rowCount().

    if you're using the mysql-functions, there's mysql_affected_rows().

    EDIT:
    seems like you're using the mysql-functions. mysql_num_rows, wich is what you're using, returns the length of your result set (for SELECT-Statements for example). what you need to use is mysql_affected_rows (as already said).

    0 讨论(0)
  • 2020-12-09 20:33

    You needs mysql_affected_rows

    0 讨论(0)
提交回复
热议问题