mysql - How to parse 1,2,3 string to int with PHP -


me 1, int (coma) 2 (int), (coma) .. from a string 1,2,3 and this is a parameter of the function that is selected from

  where the ID is from the ID ([that string ]); Prepare the statement with  

PDO .

I tried to explode it and then tried to make another variable, but still I should call STRING 1 (anti), (coma) 2 (int), (coma) < / Code> is not a string because STRING does not work here

  SELECT * to the table WHERE id ([that string]);  

You are right, IN ('1,2,3') It is not, as is IN (1,2,3) . So you can not use only one string.

If you ensure 100% that there are integers in the string and there is no chance in it, other inputs (for example, if your code string is created with those values ​​are sure to be an integer) , You can simply repeat the list in your query.

But I like to use the prepared statement when there is no guarantee that the string only is an integer.

You can create a prepared statement in which many parameters count the elements in your list.

  function afunc ($ string) {$ array_of_values ​​= explode (",", $ string); $ Array_of_placeholders = array_fill (1, calculation ($ value), "?"); $ String_of_placeholders = intestine (",", $ array_of_placeholders); $ Sql ​​= "SELECT * to table WHERE id ($ string_of_placeholders)"; $ Stmt = $ pdo- & gt; Ready ($ sql); $ Stmt- & gt; Execute ($ array_of_values); }  

MySQL does not care that the parameter value is string or integer even if you use bindParam ($ variable, PDO :: PARAM_INT) If you read the code for the PDO_mysql driver, then type logic such as PDO :: PARAM_INT is ignored, and all the parameters have been passed to the MySQL server as a string. Since they are in an integer-comparative context, the SQL parser converts them into integers (and strips any non-numeric part).


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -