I have read up how parameterised queries are the way forwards regarding prevention of SQL injection, however to my surprise it seems more complicated to implement than I had imagined.
I have attempted a simple SELECT statement using mysqli in PHP using a parameterised query and I am receiving a fatal PHP error, stating that I have called an unknown method when trying to retrieve the result of the query. After Googling it states that I need mysqlnd
installed and enabled to retrieve the result. I also found that mysqlnd
should be installed by default on PHP 5.4 or later. I am currently using 5.4.3 though, so I expected it to work.
Firstly, do I need mysqlnd
to use parameterised queries or is this only necessary for more advanced users and queries? Is there a method you can use without mysqlnd to retrieve the result? Secondly, if I do need it, how do I enable it in my PHP?
The PHP:
$stmt = $con->prepare("SELECT some_id FROM some_table WHERE name = ? AND user_id = '$user_id'");
$stmt->bind_param('s', $name);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// do something with result
}
The error:
PHP Fatal error: Call to undefined method mysqli_stmt::get_result()