php - How to convert a string into a usable array -


this question has answer here:

right i'm attempting make mysqli class make typing code easier , looks cleaner , more usable overall.

i'm attempting write function execute query prepared statement. dilemma this:

  public function saferead($query, $data, $params)   {     $stmt = $mysqli->prepare($query);     $stmt->bind_param($params, $data);     $stmt->execute();     $result = $stmt->get_result();     $check = $result->fetch_assoc();         } 

i of course want execute query, can see. problem lies $data variable. how can i/is possible pass data, string , possibly convert array or usable can used bind_param ?

bind_param prototype looks this:

bool mysqli_stmt::bind_param ( string $types , mixed &$var1 [, mixed &$... ] ) 

so accepts string of types sssd, , bunch of variables types

assuming passing in correct type of variables

$stmt->bind_param($params, $data); 

a way be

   public function saferead($query, $data, $params)    {     $stmt = $mysqli->prepare($query);     $params = str_split( $params ); // split params 'sssd'     foreach( $data $k => $v ) {         $stmnt->bind_param( $params[$k], $data[$k] );     }     $stmt->execute();     $result = $stmt->get_result();     $check = $result->fetch_assoc();         } 

this untested.


edit

since second parameter of bind_param passed reference may need create intermediate variable before binding, instead of binding array item.

foreach( $data $k => $v ) {     $var_name = 'var'.$k;     $$var_name = $v;     $stmnt->bind_param( $params[$k], $$var_name ); } 

but im not 100% sure.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -