How to create a secure mysql prepared statement in php?

前端 未结 6 572
夕颜
夕颜 2020-11-22 03:29

I am new to using prepared statements in mysql with php. I need some help creating a prepared statement to retrieve columns.

I need to get information from different

6条回答
  •  無奈伤痛
    2020-11-22 04:04

    You can write this instead:

    $qry = "SELECT * FROM mytable where userid='";
    $qry.= mysql_real_escape_string($_GET['userid'])."' AND category='";
    $qry.= mysql_real_escape_string($_GET['category'])."' ORDER BY id DESC";
    

    But to use prepared statements you better use a generic library, like PDO

    prepare('SELECT * FROM mytable where userid=? and category=? 
                          order by id DESC');
    $sth->execute(array($_GET['userid'],$_GET['category']));
    //Consider a while and $sth->fetch() to fetch rows one by one
    $allRows = $sth->fetchAll(); 
    ?>
    

    Or, using mysqli

    
    

提交回复
热议问题