Reputation: 89
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
Reputation: 16963
The solution would be like this:
$param
array and loop through your argument variable array $variables
using a for
loop.$variables
array as a reference.array_unshift()
function to push $datatype
to the beginning of $variables
array.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
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:
Upvotes: 2
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