Parameterized Queries

拥有回忆 提交于 2019-12-31 05:21:09

问题


I am currently learning parametrized queries as there are advantages to using them.

Could someone give some pointers by converting this block of code to a parametrized version?

Thanks.

if(isset($_GET['news_art_id']) && (!empty($_GET['news_art_id'])))
{
    $news_art_id = htmlentities(strip_tags($_GET['news_art_id']));
    $news_art_id = validate_intval($news_art_id);

    //echo $news_art_id;
    $_SESSION['news_art_id'] = $news_art_id;

    // Assign value to status.
    $onstatus = 1;
    settype($onstatus, 'integer');

    $query = 'SELECT M.id, M.j_surname, M.j_points_count, M.j_level, A.j_user_id,A.id, A.jart_title, A.jart_tags, A.jart_description, A.jart_createddate FROM jt_articles A, jt_members M WHERE M.id = A.j_user_id AND A.id = ' . check_db_query_id($news_art_id) . " AND A.jart_status = $onstatus;";

    $result = mysql_query($query) or die('Something went wrong. ' . mysql_error());  
    $artrows = mysql_num_rows($result);
}

回答1:


in your case there is no advantage, remember a parameterised query requires 2 calls to the db : one to setup the query template and parse, the other to populate the query template params and is typically used when looping. So in this instance you're better off calling a stored procedure (always the best choice) or using inline sql and making sure you use http://php.net/manual/en/function.mysql-real-escape-string.php when applicable.




回答2:


The general rule is: Every variable should be binded, no inline variables at all.

Technical details: http://php.net/manual/en/pdo.prepare.php



来源:https://stackoverflow.com/questions/4461759/parameterized-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!