I am trying to prevent sql injection and according to what I've been searching, the use of parameterized queries can help. Below is my PHP code on displaying the books saved in the database basing on its status which is an integer (0 for unavailable and 1 for available):
public function getBooksByStatus(){
$qry=$this->stmt_init();
if($qry->prepare("SELECT bookID, title, status FROM books WHERE status= ? ORDER By title ASC")){
$qry->bind_param("i",$_GET[id]); //$_GET[id] refers to the status
$qry->execute();
echo json_encode($qry->fetch_all(MYSQL_ASSOC));
return;
}
}
After testing the code, it doesn't have any error but it doesn't return anything.
This was my code before without parameterizing queries and actually, it works:
$qry = $this->query("SELECT bookID, title, status FROM books WHERE status= $_GET[id] ORDER By title ASC");
echo json_encode($qry->fetch_all(MYSQL_ASSOC));
return;
Can anyone help me, please?
$this->stmt_init? Or what class are you in ?return;statement?print_r( $qry->errorInfo() )and paste the result, please.