note 117760 deleted from mysqli-stmt.bind-result by cmb

From: Date: Tue, 04 Aug 2015 13:11:45 +0000
Subject: note 117760 deleted from mysqli-stmt.bind-result by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-203262@lists.php.net to get a copy of this message
Note Submitter: bboymarco ---- I did for myself a really useful function (cause I didn't want to use eval()) that create for you a prepared statement with an indefinite number of parameters and return an indefinite number of result columns. Last two arguments are optional so when you don't have parameters to send to MySQL you can avoid them. I decided to share it here cause it may be helpful and save some time and brainstorming: function prepared_stmt($con, $query, $type = "", $param = array()){ if($stmt = mysqli_prepare($con, $query)){ $refarg = array($stmt, $type);//First two parameter of mysqli_stmt_bind_param foreach ($param as $key => $value)//create array of parameters' references $refarg[] =& $param[$key]; if($type != "")//Jump instruction if argument $type is missing call_user_func_array("mysqli_stmt_bind_param", $refarg);//bind parameters with dinamic length if(mysqli_stmt_execute($stmt)){//check if execution go fine $cols = mysqli_field_count($con);//retrive the number of columns of the resultset $result = array_fill(0, $cols, NULL);//create an empty array with the same length of the columns $ref = array($stmt);//first argument of mysqli_stmt_bind_result foreach ($result as $key => $value)//create array of empty cells' references $ref[] =& $result[$key]; call_user_func_array("mysqli_stmt_bind_result", $ref);//bind results with dinamic length return $ref;//return statement and columns references }//if else return false; }//if else return false; }//prepared_stmt Example of call: $hello = prepared_stmt($con, "SELECT Example FROM table WHERE Col=?", "s", array("LOL"); //count($hello) is equal to 2 //$hello[0] Statement reference //$hello[1] Example column field where to find values after fetching while(mysqli_stmt_fetch($hello[0])) echo $hello[1] . "<br>";//This will print all value of the Example column

« previous php.notes (#203262) next »