note 117760 added to mysqli-stmt.bind-result
| From: | bboymarco | Date: | Tue, 04 Aug 2015 12:08:03 +0000 |
| Subject: | note 117760 added to mysqli-stmt.bind-result | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-203260@lists.php.net to get a copy of this message | ||
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
----
Server IP: 72.52.91.14
Probable Submitter: 188.153.106.159
----
Manual Page -- http://php.net/manual/en/mysqli-stmt.bind-result.php
Edit -- https://master.php.net/note/edit/117760
Del: integrated -- https://master.php.net/note/delete/117760/integrated
Del: useless -- https://master.php.net/note/delete/117760/useless
Del: bad code -- https://master.php.net/note/delete/117760/bad+code
Del: spam -- https://master.php.net/note/delete/117760/spam
Del: non-english -- https://master.php.net/note/delete/117760/non-english
Del: in docs -- https://master.php.net/note/delete/117760/in+docs
Del: other reasons-- https://master.php.net/note/delete/117760
Reject -- https://master.php.net/note/reject/117760
Search -- https://master.php.net/manage/user-notes.php