mysql_real_escape_string(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_real_escape_string(3) β€” MariaDB Connector/C, bestie edition

okay so real quick, this page lives in the MariaDB Connector/C manual, and it's about mysql_real_escape_string β€” the glow-up your strings need before they go anywhere near an SQL statement. πŸ¦‹


✨ NAME ✨

mysql_real_escape_string β€” escapes a string while taking into account the character set of the connection. periodt.


🌸 SYNOPSIS 🌸

#include <mysql.h>

unsigned long mysql_real_escape_string(MYSQL * mysql,
                                       char * to,
                                       const char * from,
                                       unsigned long);

πŸ’– DESCRIPTION πŸ’–

So the whole point of this icon: it builds a legal SQL string you can actually drop into an SQL statement without it all going sideways. You hand it a string, it hands you back an escaped SQL string, and it does that while respecting the current character set of the connection. no cap, that charset awareness is literally the whole flex here.


πŸŽ€ PARAMETER πŸŽ€

the cast of characters, bestie:

  • mysql β€” a mysql handle. it's gotta be one that was previously allocated by mysql_init(3) and then connected by mysql_real_connect(3). no handshake, no service. πŸ™ˆ
  • to β€” the buffer where the encoded string goes. and listen, the size of this buffer must be length * 2 + 1 bytes. why? because in the worst case every single character of the from string needs to be escaped. plus a trailing 0 character gets appended. so yes, that + 1 is doing real work, do not skip it πŸ’€
  • from β€” the string that mysql_real_escape_string() is gonna encode.
  • long β€” the length of the from string.

🫢 RETURN VALUE 🫢

Returns the length of the encoded (to) string. that's it, that's the gift.


🌈 SEE ALSO 🌈


⭐ cute sign-off ⭐

and that's the whole page, bestie πŸ’œ this one's from Version 3.4 of the MariaDB Connector/C manual β€” go forth and escape responsibly, mwah πŸŽ€βœ¨

πŸ“¦ where this page comes from