php - MySQLi Dynamic Prepared Statements -


i'm trying create dynamic function execute mysqli queries.

here example of regular query:

public function myfunc() {     $name = "ryan";      $stmt = $this->db->prepare("select name, age, id users name = ?");     $stmt->bind_param( "s", $name );     $stmt->execute();     $stmt->bind_result( $name2, $age, $id );     while( $stmt->fetch() )         $users[] = array( 'name' => $name2, 'age' => $age, 'id' => $id );     $stmt->close();     return $users; } 

i trying along lines of:

public function query( $myquery, $params, $types, $returns ) {     $stmt = $this->db->prepare( $myquery );     $stmt->bind_param( $types, $params );     $stmt->execute();     $stmt->bind_result( $returns_array );      $i = 0;     while( $stmt->fetch() ) {         $users[] = array( $returns[$i] => $returns_array[$i++], .... );     }     $stmt->close();     return $users; } 

where dynamically use prepared statements in simple use function..

an example of calling it:

$users = query( "select name, age, id users name = ?", array('ryan'), "s", array('name','age','id')); 

then call data via:

$users[0]['name'].. $users[0]['age'].. $users[0]['id'].. 

i'm little confused on how accomplish this, guys have ideas or places start? code helpful.

i found article: http://www.pontikis.net/blog/dynamically-bind_param-array-mysqli

but don't want specific on software installed on os.

update #2 need return key -> value of array of multiple arrays. came cannot seem achieve it. values of id, not things requested.

public function query( $query_string, $params = null, $param_types = null) {      //prepare statement     $stmt = $this->db->prepare($query_string);      //check if sql query wrong.     if($stmt === false) {         echo "wrong sql: " . $query_string . "<br />error: " . $this->db->errno . " " . $this->db->error;     }      if($params != null && $param_types != null) {          //build array needs pass args bind_param.         $a_params = array();         $a_params[] = &$param_types;          for($i=0; $i<count($params); $i++)             $a_params[] = &$params[$i];          // $stmt->bind_param('s', $param); equivalent         call_user_func_array(array($stmt, 'bind_param'), $a_params);     }      //run query     $stmt->execute();      //gather column names     $metadata = $stmt->result_metadata();      $field_names = array();     $fields = array();     $results = array();     $i = 0;      // tricky bit dynamically creating array of variables use     // bind results     while( $field = $metadata->fetch_field() ) {         //for binding results         $var = $field->name;         $null_v = null;         $fields[$var] = &$null_v;          //for storing field names         $field_names[$i] = $var;         $i++;     }      // bind results                                          call_user_func_array(array($stmt,'bind_result'),$fields);      //put results array     $i = 0;     while ($stmt->fetch()) {          for( $k = 0; $k < count($fields); $k++){              $results[$i][$field_names[$k]] = $fields[$field_names[$k]];         }          $i++;      }      $stmt->close();      return $results; } 

and how i'm calling , retrieving data.

$users = $tcs->query("select name, age, id test");  foreach($users $user) {      echo $user['name']; } 

but outputting id's. (1, 2, 3) should outputting names (ryan, stephen, , john)


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -