I've seen a couple of questions over the last few days that have used mysqli
but where the answers appear to have not recognised the difference between $stmt->execute()
and $db->query()
.
As I understand it, there are two differing models for accessing results in mysqli
.
This one uses raw SQL and requires the programmer to escape the input to prevent SQL injection attacks, but allows the programmer to retrieve an associative array (or a plain array) containing column => value
mappings:
$result_set = $db->query("SAFE SQL QUERY HERE");
while ($row = $result_set->fetch_assoc()) {
# do something with $row['fieldname'];
}
Alternatively, you can do this, which allows nice binding of parameters and results, but cannot (AFAIK) give you any sort of simple array result:
$stmt = $db-prepare("SQL QUERY WITH ? PLACEHOLDERS");
$stmt->bind_param("s", $input_variable);
$stmt->execute();
$stmt->bind_results($output_col1, $output_col2);
while ($stmt->fetch()) {
# do something with $output_col1 and $output_col2
}
My question is - is there any way with mysqli
to get the simple array output shown in the first pattern, but whilst still using safely bound input parameters as per the second pattern?
I can't find anyway to do this (other than using PDO
instead!).