How does one pass a random number of parameters for a query? The code blocks shown below show whether the attempt was made. All failures display Bull (false) For testing, three integer IDs are used to represent.
The query from item 1 was manually executed in pgAdminIII, and was successful. The query was echoed and compared to item 2 and 4. They look closer to seeing, but the parameters are not binding. Select column from
table WHERE id = any ('{1,2,3}' :: int []); Select column from the table WHERE id = ANY ('{?,?,?}' :: int []) 1) This works is the desire that any section 1- N number will be:
$ sql = "Select column from table WHERE id = any ('{1,2,3}' :: int [])"; $ Sth = $ dbh- & gt; Ready ($ sql); $ Sth- & gt; Executed (); $ Result = $ sth- & gt; Get (PDO :: FETCH_NUM); Echo var_dump ($ result); $ Dbh = null; 2) This fails:
$ value = array (1,2,3); $ PlaceHolders = implode (',', array_fill (0, count ($ value), '?')); $ Sql = sprintf ("The table from the selection column WHERE id = any ('{% s}' :: int [])", $ placeHolders;); $ Sth = $ dbh- & gt; Ready ($ sql); $ Sth- & gt; Execute ($ value); $ Result = $ sth- & gt; Fetch (); Echo var_dump ($ result); $ Dbh = null; 3, it fails: $ value = array (': qwer' = & gt; 1, ': asdf' => 2, '': Zxcv '= & gt; ; 3); $ Sql = "table WHERE id = any from the column column = any (\ '{: qwer,: asdf,: zxcv} \' :: int [])"; // Do not survive a quote below if the error causes $ sth = $ dbh- & gt; Ready ($ sql); $ Sth- & gt; Execute ($ value); // Invalid parameter number :: If single quotes are not saved then qwer $ Result = $ sth- & gt; Fetch (); Echo var_dump ($ result); $ Dbh = null; 4) This failed (explicitly binding parameter):
$ value = array (1,2,3); $ PlaceHolders = implode (',', array_fill (0, count ($ value), '?')); $ Sql = sprintf ("search query nc_reports WHERE id = any ('{% s}' :: int [])", $ placeHolders;); $ Sth = $ dbh- & gt; Ready ($ sql); $ I = 1; Forex currency ($ value as val $) {// $ sth- & gt; Bind Parm ($ i, $ val, PDO :: PARAM_INT); $ Sth- & gt; Bind value ($ i, $ val, PDO :: PARAM_INT); $ I ++; } $ Sth- & gt; carry about (); $ Result = $ sth- & gt; Fetch (); Echo var_dump ($ result); $ Dbh = null;
Try to generate the query yourself:
$ Value = array (1,2,3); // generate query $ params = substr (str_repeat ("?,", Sizeof ($ value)), 0, -1); $ Sql = "nc_reports, select the query where id ($ params)"; $ Sth = $ dbh- & gt; Ready ($ sql); ($ I = 0; $ i & lt; Size ($ value); $ i ++) {$ sth- & gt; Byte value ($ i + 1, $ value [$ i], PDO :: parametti); } $ Sth- & gt; carry about (); // Repeat during the result ($ result = $ sth-> fetch ())! == FALSE {echo var_dump ($ result); } $ Dbh = null;
No comments:
Post a Comment