lawrence.adu
lawrence.adu

Reputation: 89

Multiple Arguments as one Argument in php

I'm writing a function that takes three arguments, the first two arguments are strings but I want the third argument to comprise of multiple arguments. Below is the block of code

function myFetch($query, $datatype, $variables){
        include("models.php");
        $stmt = $conn->prepare($query);
        $stmt->bind_param($datatype, $variables);
        $stmt->execute();
        $result = $stmt->get_result();
        return $result->fetch_assoc();

        $stmt->close();
        $conn->close();
}

From the above code, I'd like the third argument $variable to contain other arguments, example $variable = $one, $two, $three, $four of which these arguments have their own data to be passed to the bind_param function.

Example of the $query, $datatype and $variables:
$query = "SELECT * FROM table WHERE id = ? and visible = ?";
$datatype = "ii";
$id = 1,$visible = 1;
$variables = $id, $visible;

Upvotes: 1

Views: 106

Answers (3)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

The solution would be like this:

  • Create an empty $param array and loop through your argument variable array $variables using a for loop.
  • In each iteration of the loop, push the elements to $variables array as a reference.
  • Use array_unshift() function to push $datatype to the beginning of $variables array.
  • Finally, use call_user_func_array() function to bind each of the parameters.

So your myFetch() function should be like this:

function myFetch($query, $datatype, $variables){
    include("models.php");
    $stmt = $conn->prepare($query);
    $param = array();
    $count = count($variables);
    for($i = 0; $i < $count; ++$i){
        $param[] = &$variables[$i];
    }
    array_unshift($param, $datatype);
    call_user_func_array(array($stmt, 'bind_param'), $param);
    $stmt->execute();
    $result = $stmt->get_result();
    $stmt->close();
    $conn->close();

    return $result;
}

Subsequently, you can call this function like this:

$query = "SELECT * FROM table WHERE id = ? and visible = ?";
$datatype = "ii";
$variables = array(1, 1);

$result = myFetch($query, $datatype, $variables);
while($row = $result->fetch_assoc()){
    // display $row details
}

Upvotes: 2

Oliver
Oliver

Reputation: 883

This should do everything you want in your function:

$query = "SELECT * FROM table WHERE id = ? and visible = ?";
$variables = array($id, $visible);
function myFetch($query, $variables){
        include("models.php");
        $stmt = $conn->prepare($query);
        $stmt->execute($variables);
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

Just some thoughts:

  • Could you use include_once outside your function instead of include?
  • When you return a value before you close your connection, it will never get closed

Upvotes: 2

leoap
leoap

Reputation: 1729

Use Reflection to call mysqli::bind_param(), Example:

<?php
$resource = $db->prepare("SELECT * FROM table WHERE id = ? and visible = ?");
$reflectionArray = array(1,"yes");
$reflection = new ReflectionClass('mysqli_stmt');
$method = $reflection->getMethod("bind_param");
$method->invokeArgs($resource, $reflectionArray);
$resource->execute(); 
?>

Your function:

function myFetch($query, $datatype, $variablesArray){
    include("models.php");
    $stmt = $conn->prepare($query);

    $reflection = new ReflectionClass('mysqli_stmt');
    $method = $reflection->getMethod("bind_param");
    $method->invokeArgs($stmt, $variablesArray);
    $resource->execute(); 

    $result = $stmt->get_result();

    $stmt->close();
    $conn->close();

    return $result->fetch_assoc();
}

Upvotes: 1

Related Questions