cypher86 Posted November 9, 2011 Share Posted November 9, 2011 $params = array(); $type=array(); $fragments = array(); while (list($chiave,$val) = each($_POST)){ if($val!=""){ $fragments[] = $chiave." = ?"; //echo $chiave; if($chiave=="ritiro" or $chiave=="data") $params[] = normalToDbDate($val); else $params[] = $val; if($chiave=="legale") $type[]='i'; else $type[]='s'; } } $prova=implode("",$type); foreach($params as $param) echo "<p>".$param."</p>"; //echo "val(".count($params).")-->".implode(',',$params); //echo "type(".strlen($prova).")-->".implode("",$type); $sql = $db->prepare("..... AND ".implode(" AND ", $fragments)); array_unshift($params,$prova) call_user_func_array( array( $sql, 'bind_param' ),$params); //$sql->bind_param($prova,$params); $sql->execute(); hi all, with the tructure above i've tried to implement the cration of a dynamic query for a search form. i debugged with echoes and che numbers of bind_param and the those in the prepared statement match in sense that count($params)=strlen($prova). having said that i get this error: Warning: call_user_func_array() expects parameter 2 to be array, integer given in and no results are shown to me even when the filter match. Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/ Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 looks like you may need to read the manual for this one.. http://us3.php.net/manual/en/function.call-user-func-array.php Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286558 Share on other sites More sharing options...
cypher86 Posted November 9, 2011 Author Share Posted November 9, 2011 why?? $params is an array.... Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286565 Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 apparently it's not, may want to double check that. Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286567 Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 i spotted the issue, array_unshift($params,$prova). this will output an integer for $params, you then proceed to use it as an array Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286569 Share on other sites More sharing options...
cypher86 Posted November 9, 2011 Author Share Posted November 9, 2011 foreach($params as $param) echo "<p>".$param."</p>"; $params seems an array (at list for this loop). so call_user_func_array( array( $sql, 'bind_param' ),$params); should act like $sql->bind_param('iiiiss....',$par1,$par2,.......) or am i wrong? edit: according to http://php.net/manual/en/function.array-unshift.php it returns an array but at the head of the array is added the second, third,... param Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286570 Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 no, accordning to the manual, it returns the number of elements in the array.. quote from manual: Return Values Returns the new number of elements in the array. Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286572 Share on other sites More sharing options...
cypher86 Posted November 9, 2011 Author Share Posted November 9, 2011 sure i wrote wrong it returns an int but the $params should remain an array added with $prova at the beginning. do you confirm? edit: below the output of print_r($params): Array ( [0] => i [1] => 16 ) so it seems an array. Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286573 Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 since you are not setting the return value of array_unshift to $params, I do agree that the $params value should still be an array, I would try outputting either a var_dump or a print_r just after using the array_unshift function and before using the call_user_func_array function Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286576 Share on other sites More sharing options...
cypher86 Posted November 9, 2011 Author Share Posted November 9, 2011 i thought the same thing so i edited above Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286580 Share on other sites More sharing options...
AyKay47 Posted November 9, 2011 Share Posted November 9, 2011 the only other thing that I see here is that you have forgotten a semi colon after your call to array_unshift. Other than that I am stumped on this one Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286592 Share on other sites More sharing options...
cypher86 Posted November 9, 2011 Author Share Posted November 9, 2011 all right then... do you have any suggestion in order to implement a search form?? Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286619 Share on other sites More sharing options...
cypher86 Posted November 9, 2011 Author Share Posted November 9, 2011 the form i'm working on is the following: <fieldset id="ricerca" name="ricerca"> <legend>Ricerca</legend> <form name="form_notifiche" id="form_notifiche" action="notifiche.php?cerca=true" method="POST"> <input type="hidden" id="id" name="id" value="" /> <div><label>Data: </label><input type="text" name="data" id="data" /><input type="button" value="..." onclick="return showCalendar('data', '%d/%m/%Y');">(gg/mm/aaaa)</div> <div><label>Numero: </label><input type="text" name="numero" id="numero" /></div> <div><label>Parti: </label><input type="text" name="parti" id="parti" /></div> <div><label>Legale: </label><select id="legale" name="legale" ><option value="16">Alberto</option><option value="2">Alberto</option><option value="15">Alberto</option><option value="9">cypher</option>.......</select></div> <div><label>Atto: </label><input type="text" name="atto" id="atto" /></div> <div><label>Data Ritiro: </label><input type="text" name="ritiro" id="ritiro" /><input type="button" value="..." onclick="return showCalendar('ritiro', '%d/%m/%Y');">(gg/mm/aaaa)</div> <div><input type="submit" value="Cerca"></div> </form> </fieldset> Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286632 Share on other sites More sharing options...
cypher86 Posted November 10, 2011 Author Share Posted November 10, 2011 there is any other way to implement a search using the form above??? Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1286977 Share on other sites More sharing options...
AyKay47 Posted November 10, 2011 Share Posted November 10, 2011 typically for a search form, the user types in the search criteria, and a SELECT query using the LIKE clause is used to pull results from the database and display the results to the user.. there are many tutorials out there that can get your started if you are not sure on how to approach this. Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287031 Share on other sites More sharing options...
cypher86 Posted November 10, 2011 Author Share Posted November 10, 2011 i agree if you search on a text. what about date and int? do you have nay suggestion for a search on a form like the one i've posted? Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287032 Share on other sites More sharing options...
AyKay47 Posted November 10, 2011 Share Posted November 10, 2011 with dates you will use a conditional statement in your query, do you want to pull dates that are greater than or less than the date specified by the user. An integer will need to come back with an equivalent match, what exactly do you mean by your question? Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287034 Share on other sites More sharing options...
cypher86 Posted November 10, 2011 Author Share Posted November 10, 2011 i mean that if the method i've proposed is not fixable i should build a query for every possible combination and those are a lot of combinations..... Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287039 Share on other sites More sharing options...
AyKay47 Posted November 10, 2011 Share Posted November 10, 2011 just need one query Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287047 Share on other sites More sharing options...
cypher86 Posted November 10, 2011 Author Share Posted November 10, 2011 given the forum i've posted can you help me no that pls??? Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287049 Share on other sites More sharing options...
Adam Posted November 10, 2011 Share Posted November 10, 2011 Did you try adding some debugging between the array_unshift() and call_user_func_array() calls? Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287050 Share on other sites More sharing options...
cypher86 Posted November 10, 2011 Author Share Posted November 10, 2011 sure, and everything looks fine but it does not work at the end.... do you have any suggestion?????????? Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287053 Share on other sites More sharing options...
Adam Posted November 10, 2011 Share Posted November 10, 2011 Well as AyKay47 pointed out, you were missing a semi-colon off the end of your array_unshift() call, which means the code you posted would never work. Can you post the exact code you're running when you're receiving the error? Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287058 Share on other sites More sharing options...
cypher86 Posted November 10, 2011 Author Share Posted November 10, 2011 $params = array(); $type=array(); $fragments = array(); //while (list($chiave,$val) = each($_POST)){ // echo $chiave.":".$val."<br>"; //} while (list($chiave,$val) = each($_POST)){ if($val!=""){ $fragments[] = $chiave." = ?"; //echo $chiave; if($chiave=="ritiro" or $chiave=="data") $params[] = normalToDbDate($val); else $params[] = $val; if($chiave=="legale") $type[]='i'; else $type[]='s'; } } $query = "....... AND ".implode(" AND ", $fragments); echo $query; $prova=implode("",$type); foreach($params as $param) echo "<p>".$param."</p>"; //echo "val(".count($params).")-->".implode(',',$params); //echo "type(".strlen($prova).")-->".implode("",$type); $sql = $db->prepare("....... AND ".implode(" AND ", $fragments)); array_unshift($params,$prova); print_r($params); call_user_func_array( array( $sql, 'bind_param' ),$params); //$sql->bind_param($prova,$params); $sql->execute(); Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287078 Share on other sites More sharing options...
AyKay47 Posted November 10, 2011 Share Posted November 10, 2011 I have just noticed something, you have not encased your foreach code in curly brackets. Quote Link to comment https://forums.phpfreaks.com/topic/250765-warning-mysqli_stmtbind_param-mysqli-stmtbind-param/#findComment-1287081 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.