I am trying to create a function in PHP that accepts two arguments, one being the SQL for a mysqli query and the other being the name I would like to be set as the variable name for the resulting array. Here it is so far:
<?php
function dbSelect($sql,$name) {
include "connect.php";
if($results = $db->query($sql)) {
if($results->num_rows) {
while($row = $results->fetch_object()) {
${$name}[] = $row;
}
$results->free();
}
}
return ${$name};
}
Within the referenced connect.php file is this code
$db = new mysqli('127.0.0.1', 'admin', 'PASSWORD', 'DB_NAME');
However it does not seem to work in its current state. I do not get any errors when calling the function "dbSelect($sql, 'test');" however when I try to reference the supposedly created variable (in this case, $test) I get an undefined error.
Any suggestions or tips on how to fix this?
Thanks in advance.
$name[] = $row; ... return $name;?