Getting the number of rows affected by UPDATE in PostgreSQL

variants of this question are asked on SO and in many blogs, but none of them give a direct answer. I hope there is one. I am updating PostgreSQL 9.0 (from CodeIgniter, PHP framework):

$sql_order = "UPDATE meters SET billed=true"; $query = $this->db->query($sql_order); 

I just need the number of rows affected by the update, but it seems to be impossible with PostgreSQL. The query now returns boolean - true. Manual and web-based conversations refer to the RETURNING , GET DIAGNOSTICS syntax, and the default return type from UPDATE . I could not get them to work. Is there an easy way to get strings to affect the count without inserting this simple operation into a procedure or transaction.

+6
source share
1 answer

In Java, I would use the following:

 Statement stmt = connection.createStatement(); int rowsAffected = stmt.executeUpdate("UPDATE ..."); 

In PHP, I consider pg_affected_rows . And in your specific case $this->db->affected_rows()

+8
source

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


All Articles