I was just wondering if there was a way I could use some form of prepared statements in MySQL so I wouldn’t have to escape all my inputs and I wouldn’t have to switch all of my files from MySQL to MySQLi. I really don’t trust the escaping functions, so if there is any alternatives that work in regular MySQL, it would be great.
Share
Use PDO (PHP Data Objects) to connect to your MySQL database. This method will make sure that all database input will always be treated as text strings and you will never have to do any manual escaping.
This combined with proper use of html_entities() to display data from your database is a solid and good way to protect your page from injection. I always use PDO to handle all my database connections in my projects.
Create database object (and in this case enforce a certain character encoding):
Then use it like so:
or
and with wildcards:
Read more:
How can I prevent SQL injection in PHP?
When *not* to use prepared statements?
how safe are PDO prepared statements
http://php.net/manual/en/book.pdo.php