I was wondering if it's possible to store the variables to be inserted in a bind_param()
function inside another variable such that if this were the original query:
$stmt->bind_param("si", $column1, $column2);
would it be possible to store the 2 variables inside another variable, i.e:
$columns = $column1.','.$column2;
And then if the new variable can be used inside the bind_param()
function like this such that it works:
$stmt->bind_param("si", $columns);
My Database column names and php variable names are the same (except for the '$' sign of course), so I tried this:
$string = "column1, column2";
function convert_to_variables($string) {
// Convert column names to an array
$array = explode(", ", $string);
// Convert column names array values to a variable
foreach ($array as $key => $value) {
$array_values_with_dollar[] = $$value; // (previously tried with '$'.$value)
}
// Convert to string again
$imploded = implode(", ", $array_values_with_dollar);
return $imploded;
}
.
.
$variables = convert_to_variables($string);
.
.
$stmt->bind_param("si", $variables );
.
.
// Everything else like $stmt = $dbc->prepare(""); and $stmt->execute(); etc exists.
// The bind_param() is the only line with an error.
(Right now I'm getting an 'Undefined variable: column1' error.)