3 ms·
Maybe I'm misunderstanding you, but it looks like it is a PHP specific problem. Python does not have it. Of course at C level parameterised queries ("prepared
by nh2 5y ago
Maybe I'm misunderstanding you, but it looks like it is a PHP specific problem. Python does not have it.
Of course at C level parameterised queries ("prepared statements") do not accept arrays. It would not make sense for them to do.
This is because the placeholders can be substituted by values of different types.
In typed programming languages, arrays have elements of the same type.
For example in SQLite, you are supposed to call `sqlite3_bind_int()`, `sqlite3_bind_text()` [1], once for each query parameter.
In languages like PHP and Python, where arrays can carry values of different types, their wrappers around the SQLite C functions can do this function calling for each value in the array. In Python, that is easy, the default, and explained in the very beginning of the official standard library's sqlite documentation [2]:
It states at the very beginning that query construction by string construction is unsafe and must be avoided. It immediately provides an example of how to safely call a parameterised query with an array of values, using `execute()` and `executemany()`.
PHP's standard library simply does not seem to have such an `execute()` function that accepts an array [3], nor do the official docs seem to contain any prose that could explain how to use the library safely [4]. The only way you can find out is by reading user-contributed comments on some specific functions in the function reference.
So Python's standard library provides safe functions, and immediately instructs the user how to use them. PHP's does not. Unclear to me how one can conclude that this isn't a PHP specific problem.
[1]: https://www.sqlite.org/c3ref/bind_blob.html https://www.sqlite.org/c3ref/bind_blob.html
[2]: https://docs.python.org/3/library/sqlite3.html https://docs.python.org/3/library/sqlite3.html
[3]: https://www.php.net/manual/en/sqlite3.prepare.php https://www.php.net/manual/en/sqlite3.prepare.php
[4]: https://www.php.net/manual/en/class.sqlite3.php https://www.php.net/manual/en/class.sqlite3.php
- dreyfan 5y agoYou're misunderstanding You cannot do something like the following: "WHERE status in (...?)" and then ->execute($status_array) But you can pass an array of parameters just fine (to individually bound input placeholders). It depends on which API you're using but it's of the format: ->bind_param($types, ...$params); or ->execute($params);
- ransom1538 5y ago"Maybe I'm misunderstanding you, but it looks like it is a PHP specific problem. Python does not have it." You can concatenate a sql query just fine in Python anyway you want. Adding a sql injection is just as easy in Python, PHP, or lisp. Thus this is a choice. Nothing to do with a language. Language bashing is gross and spreads lies. And yes, you can bind an array of params in PHP.
- jve 5y agoActually if you use .NET EF Core 2 FromSql or ExecuteSqlCommand, you get parametrized queries from string interpolation for free: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0/#string-interpolation-in-fromsql-and-executesqlcommand https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core...
- yourenotsmart 5y ago> Maybe I'm misunderstanding you, but it looks like it is a PHP specific problem. Python does not have it. I don't know how carefully you read what I said, if you misunderstood that this is a native library limitation (C level) of the actual database clients, and that most of the popular PHP libraries can bind arrays.