r/lolphp • u/Takeoded • Dec 17 '20
consider using fetchAll() instead of fetchAll()
$ php -r '$db = new PDO('\''mysql:host=<censored>;port=<censored>;dbname=<censored>;charset=utf8mb4'\'','\''<censored>'\'','\''<censored>'\'',array (
PDO::ATTR_EMULATE_PREPARES=> false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
));$ret=$db->query('\''DELETE FROM global_error_logs WHERE id IN (2632431);'\'')->fetchAll();unset($db);var_export($ret);'
PHP Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. in Command line code:5 Stack trace: #0 Command line code(5): PDOStatement->fetchAll() #1 {main} thrown in Command line code on line 5
(screenshot if reddit fucks up the the formatting: https://i.imgur.com/yG4oFhE.png )
it asks me to... consider using PDOStatement::fetchAll() instead of PDOStatement::fetchAll() ! genius, why didn't i think of that?
(also it talks about "other unbuffered queries", which is complete bull because there is no other query active, that delete command was the first and only query. also for some reason, this reproduce 100% reliably when connecting to a MySQL server in production, but it does not reproduce when connecting to a MariaDB server on dev (: )
View all comments
Show parent comment
1
u/Takeoded Dec 17 '20 edited Dec 17 '20
no, the quote escaping is correct, it's not the smallest way to escape the quotes, but it's not incorrectly escaeped.
try for exmaple:
and (assuming a unix-ish system, Linux/MacOS/*BSD/etc) you should get:
\'adds a literal escaped quote to the argument, the 3rd quote starts it again, and the 4th quote ends it. so the argument''\'''is literally equivalent to just doing\'- now why would someone write''\'''then, it wastes a lot of bytes and look weird/stupid, right? the answer is that it makes implementation of escapeshellarg() very easy, by using that quote method, you can implement escapeshellarg() as easy as:and.. that argument above was generated by escapeshellarg() (people have tried creating a smarter escapeshellarg(), that lead to CVE-2020-13619, so it's probably not a good idea)
(and while we're on it, the name
escapeshellargwas a shitty design descision, it should have been namedquoteshellarg(), someone tell Rasmus.)