arrays - How to bind mysqli bind_param arguments dynamically in PHP? -
i have been learning use prepared , bound statements sql queries, , have come out far, works ok not dynamic @ when comes multiple parameters or when there no parameter needed,
public function get_result($sql,$parameter) { # create prepared statement $stmt = $this->mysqli->prepare($sql); # bind parameters markers # not dynamic enough... $stmt->bind_param("s", $parameter); # execute query $stmt->execute(); # these lines of code below return 1 dimentional array, similar mysqli::fetch_assoc() $meta = $stmt->result_metadata(); while ($field = $meta->fetch_field()) { $var = $field->name; $$var = null; $parameters[$field->name] = &$$var; } call_user_func_array(array($stmt, 'bind_result'), $parameters); while($stmt->fetch()) { return $parameters; //print_r($parameters); } # close statement $stmt->close(); }
this how call object classes,
$mysqli = new database(db_host,db_user,db_pass,db_name); $output = new search($mysqli);
sometimes don't need pass in parameters,
$sql = " select * root_contacts_cfm "; print_r($output->get_result($sql));
sometimes need 1 parameters,
$sql = " select * root_contacts_cfm root_contacts_cfm.cnt_id = ? order cnt_id desc "; print_r($output->get_result($sql,'1'));
sometimes need more 1 parameters,
$sql = " select * root_contacts_cfm root_contacts_cfm.cnt_id = ? , root_contacts_cfm.cnt_firstname = ? order cnt_id desc "; print_r($output->get_result($sql,'1','tk'));
so, believe line not dynamic enough dynamic tasks above,
$stmt->bind_param("s", $parameter);
to build bind_param dynamically, have found on other posts online.
call_user_func_array(array(&$stmt, 'bind_params'), $array_of_params);
and tried modify code php.net getting nowhere,
if (strnatcmp(phpversion(),'5.3') >= 0) //reference required php 5.3+ { $refs = array(); foreach($arr $key => $value) $array_of_param[$key] = &$arr[$key]; call_user_func_array(array(&$stmt, 'bind_params'), $array_of_params); }
why? ideas how can make work?
or maybe there better solutions?
found answer mysqli:
public function get_result($sql,$types = null,$params = null) { # create prepared statement $stmt = $this->mysqli->prepare($sql); # bind parameters markers # not dynamic enough... //$stmt->bind_param("s", $parameter); if($types&&$params) { $bind_names[] = $types; ($i=0; $i<count($params);$i++) { $bind_name = 'bind' . $i; $$bind_name = $params[$i]; $bind_names[] = &$$bind_name; } $return = call_user_func_array(array($stmt,'bind_param'),$bind_names); } # execute query $stmt->execute(); # these lines of code below return 1 dimentional array, similar mysqli::fetch_assoc() $meta = $stmt->result_metadata(); while ($field = $meta->fetch_field()) { $var = $field->name; $$var = null; $parameters[$field->name] = &$$var; } call_user_func_array(array($stmt, 'bind_result'), $parameters); while($stmt->fetch()) { return $parameters; //print_r($parameters); } # commented lines below return values not arrays # bind result variables //$stmt->bind_result($id); # fetch value //$stmt->fetch(); # return value //return $id; # close statement $stmt->close(); }
then:
$mysqli = new database(db_host,db_user,db_pass,db_name); $output = new search($mysqli); $sql = " select * root_contacts_cfm order cnt_id desc "; print_r($output->get_result($sql)); $sql = " select * root_contacts_cfm root_contacts_cfm.cnt_id = ? order cnt_id desc "; print_r($output->get_result($sql,'s',array('1'))); $sql = " select * root_contacts_cfm root_contacts_cfm.cnt_id = ? , root_contacts_cfm.cnt_firstname = ? order cnt_id desc "; print_r($output->get_result($sql, 'ss',array('1','tk')));
mysqli lame when comes this. think should migrating pdo!
Comments
Post a Comment