mysql_affected_rows(3)

πŸ“¦
version 13.0.2 Β· 4 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, debian trixie, fedora 43, fedora 44, nixpkgs unstable, opensuse-leap 16.0, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’… mysql_affected_rows β€” the MariaDB Connector/C glow-up

okay bestie, welcome to the <mysql.h> cinematic universe ✨ this page is mysql_affected_rows(3) from the MariaDB Connector/C manual, and it's about to tell you how many rows your last query actually touched. yes, actually touched. we'll get to the drama.


🌸 Name

mysql_affected_rows β€” returns the number of rows affected by the last operation πŸ’–

✨ Synopsis

#include <mysql.h>

my_ulonglong mysql_affected_rows(MYSQL * mysql);

πŸ¦‹ Description

So basically: this function hands you the number of affected rows from the last operation tied to mysql. It only counts if that operation was an "upsert" β€” and by that we mean an INSERT, UPDATE, DELETE or REPLACE statement. And if the last operation flopped? You get -1. not a vibe, but there it is 😭

Parameters:

  • mysql β€” a connection identifier. It's gotta be one you already allocated with mysql_init(3) and connected with mysql_real_connect(3). no sneaking in randoms, she needs to know her people 🫢

Notes:

  • When you're using UPDATE, MariaDB will not update columns where the new value is the same as the old value. The way that this matters: mysql_affected_rows may not actually equal the number of rows matched β€” it's only the number of rows that were literally affected by the query. matched β‰  affected, and she's counting affected. periodt πŸ’…
  • The REPLACE statement first deletes the record with the same primary key and then inserts the new record. So this function returns the number of deleted records in addition to the number of inserted records. two-for-one, iconic behavior πŸ‘‘

⭐ Return value

Returns the number of affected rows, or -1 on error. that's it, that's the tea.

🌈 See also


Version 3.4, MariaDB Connector/C β€” mwah, stay hydrated and always check your return values, bestie πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from