0

I want to bind a variable amount of results from a database.

$randomAmount = rand(0, 10);

for ($i = 1; $i <= $randomAmount; $i++) {
    mysqli_stmt_bind_result($databaseGet, ${'op'. $i});
}

It runs the mysqli_stmt_bind_result() function a random amount of times. What I want to achieve is the $randomAmount as variable in the parameter.

So, if $randomAmount = 4, the code should do something to achieve this:

mysqli_stmt_bind_result($databaseGet, $op1, $op2, $op3, $op4;
2
  • As per documentation, mysqli_stmt_bind_result binds variables to columns so it's not clear how it could be a variable number of columns. Commented Mar 21, 2016 at 10:57
  • I want to reuse this function. the query with amount of columns comes in as a variable. Something like this: $query = "select k1, k2, k3, k4 FROM table" $randomAmount = 4; Commented Mar 21, 2016 at 11:07

1 Answer 1

1

You may use call_user_func_array function as follow:

$vars = [$databaseGet]
for ($i = 1; $i < 6; $i++) {
    $vars[] = &${'op' . $i};
}

call-user-func-array('mysqli_stmt_bind_result', $vars);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.