php如何创建mysql事件_如何在php中创建安全的MySQL准备语句?

2ef055c3386e3fa82983ce2c115d7578.png

慕村225694

您可以这样写:$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";但是要使用准备好的语句,最好使用泛型库,如PDO<?php /* Execute a prepared statement by passing an array of values */$sth = $dbh->prepare('SELECT * FROM mytable where userid=? a

nd 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(); ?>或者,使用米斯里<?php

$link = mysqli_connect("localhost", "my_user", "my_password", "world");/* check connection */if (mysqli_connect_errno()) {

    printf("Connect failed: %s\n", mysqli_connect_error());

    exit();}$category = $_GET['category'];$userid = $_GET['userid'];/* create a prepared statement */if 

    ($stmt = mysqli_prepare($link, 'SELECT col1, col2 FROM mytable where 

                      userid=? and category=? order by id DESC')) {

    /* bind parameters for markers */

    /* Assumes userid is integer and category is string */

    mysqli_stmt_bind_param($stmt, "is", $userid, $category);  

    /* execute query */

    mysqli_stmt_execute($stmt);

    /* bind result variables */

    mysqli_stmt_bind_result($stmt, $col1, $col2);

    /* fetch value */

    mysqli_stmt_fetch($stmt);

    /* Alternative, use a while:

    while (mysqli_stmt_fetch($stmt)) {

        // use $col1 and $col2 

    }

    */

    /* use $col1 and $col2 */

    echo "COL1: $col1 COL2: $col2\n";

    /* close statement */

    mysqli_stmt_close($stmt);}/* close connection */mysqli_close($link);?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值