I think this may be a flaw in bringing the data method to my PDO,
< Code> public function Fetch_all ($ query, $ params = array ()) {{# Prepare query $ stmt = $ this- & gt; Connection- & gt; ($ Query); #If $ params do not have an array, then we make it with a value of $ parameters if (! Is_array ($ params)) $ params = array ($ params); # Execute queries $ stmt-> Execute ($ params); # Return result return $ stmt-> Get All (PDO :: FETCH_ASSOC); } Hold (PDOException $ e) {# get_error function $ this- & gt; Call get_error ($ e); }}All parameters passed in this method will be stringed, but I need integer for the SQL LIMIT query, such as below
$ Sql = "SELECT * root_pages ORDER by root_pages.pg_created DESC LIMIT ?,?"; Item = $ connection- & gt; Fetch_all ($ sql, array ('0', '6'));
It returns this error,
2SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
How can I fix this?
Edit:
As I've suggested, I code this down my own way Get more than one line of # result, public function fetch_all ($ query, $ params = array ()) as nested array (= multi-dimensional array) { {# Prepare query $ stmt = $ this- & gt; Connection- & gt; ($ Query); # If $ params is not an array, then it first creates an array with a value of $ params // if (! Is_array ($ params)) $ params = array ($ params); Forex currency ($ $ k = & gt; $ par $ params) {if (is_numeric ($ p)) {$ stmt-> Bound pattern ($ k + 1, $ p, PDO :: PARAM_INT); } And {$ stmt- & gt; Baidam Parm ($ k + 1, $ p, PDO :: Param_st); }} $ Stmt-> carry about (); Execute # Queries // $ stmt-> Execute ($ params); # Return result return $ stmt-> Get All (PDO :: FETCH_ASSOC); } Hold (PDOException $ e) {# get_error function $ this- & gt; Call get_error ($ e); }} $ Items = $ connection- & gt; Fetch_all ($ sql, array (0,6));
Then I get a different error,
2 SQLSTAT [42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax ;
Edit:
I have only changed it. I manually check that manual near the line for the correct syntax from your MySQL server version Use '6' '. ,
if (is_int ($ p)) {..}
but still the same error is happening ... ah ...
You can not do it through placeholders.
PDO casts always parameters which are not
null , even when they are integers, generally it is not such a bad thing , But the
LIMIT section can not handle quoted integers.
You need to go back to the good old fashioned containment. Because you know that these are going to be integers, you can call them before inserting or treat them in your code safely.
$ limit = intval ($ thing_that_provides_limit); $ Offset = intval ($ thing_that_provides_offset); $ Sql = "SELECT * root_pages ORDER by root_pages.pg_created DESC LIMIT {$ offset}, {$ limit}";
Comments
Post a Comment