I need to create dynamic queries for a webpage. The users can select what categories they want to search in (the column names in the SQL database) and what are the values they are looking for. I run through some similar posts on stackoverflow and other sites, but I wasn't able to find a solution. I use the code below (It's for testing, so the category I wrote in the category names and the values). In case I don't try to bind the parameters dynamicly the query works well, I can print the values of the $tester array. What could be the solution for this problem? I guess the problem is with the 'call_user_func_array...' part.
$stmt = mysqli_stmt_init($conn);
//The category names and variable names
$cat1 = "LCVAR";
$cat2 = "APOLG";
$LCVAR = "KALOCSA";
$APOLG = "magyar";
//creating the statement
$statement = "SELECT VNEV,KNEV FROM TORZS WHERE ";
$statement = $statement . " " . $cat1 . " ". "=?". " ". "AND" . " ";
$statement = $statement . " " . $cat2 . " ". "=?";
//SELECT VNEV,KNEV FROM TORZS WHERE LCVAR =? AND APOLG =?
mysqli_stmt_prepare($stmt,$statement);
$a_params = array();
$a_param_type = array("s","s");
$a_bind_params = array($cat1 => $LCVAR,$cat2 => $APOLG);
$param_type = '';
// creating array for call_user_function_array
$n = count($a_param_type);
for($i = 0; $i < $n; $i++) {
$param_type .= $a_param_type[$i];
}
$a_params[] = &$param_type;
for($i = 0; $i < $n; $i++) {
$a_params[] = &$a_bind_params[$i];
}
call_user_func_array(array($stmt,'mysqli_stmt_bind_param'),$a_bind_params);
mysqli_stmt_execute($stmt);
$result = array();
mysqli_stmt_bind_result($stmt, $result['VNEV'], $result['KNEV']);
$tester = array();
while(mysqli_stmt_fetch($stmt)){
$tester[] = array($result['VNEV'],$result['KNEV']) ;
}
EDIT 1:
Result of print_r($stmt)
:
mysqli_stmt Object
( [affected_rows] => 0
[insert_id] => 0
[num_rows] => 0
[param_count] => 2
[field_count] => 2
[errno] => 0
[error] =>
[error_list] => Array ( )
[sqlstate] => 00000
[id] => 1 )
Result of print_r($a_bind_params)
:
Array ( [LCVAR] => KALOCSA
[APOLG] => magyar )