如何在php中创建一个安全的mysql预处理语句?

chr*_*ris 27 php mysql security

我是新手在php中使用mysql中的预处理语句.我需要一些帮助来创建一个准备好的语句来检索列.

我需要从不同的列中获取信息.目前,对于测试文件,我使用完全不安全的 SQL语句:

$qry = "SELECT * FROM mytable where userid='{$_GET['userid']}' AND category='{$_GET['category']}'ORDER BY id DESC"
$result = mysql_query($qry) or die(mysql_error()); 
Run Code Online (Sandbox Code Playgroud)

有人可以使用来自url参数(如上所述)的输入来帮助我创建一个安全的 mysql语句吗?

奖励:准备好的陈述也可以提高速度.如果我只在页面上使用三到四次准备好的声明,它会提高整体速度吗?

Amb*_*ber 47

这是一个使用mysqli的例子(对象语法 - 如果你愿意,很容易翻译成函数语法):

$db = new mysqli("host","user","pw","database");
$stmt = $db->prepare("SELECT * FROM mytable where userid=? AND category=? ORDER BY id DESC");
$stmt->bind_param('ii', intval($_GET['userid']), intval($_GET['category']));
$stmt->execute();

$stmt->store_result();
$stmt->bind_result($column1, $column2, $column3);

while($stmt->fetch())
{
    echo "col1=$column1, col2=$column2, col3=$column3 \n";
}

$stmt->close();
Run Code Online (Sandbox Code Playgroud)

此外,如果您想要一种简单的方法来获取关联数组(与SELECT*一起使用)而不必准确指定要绑定的变量,这里有一个方便的函数:

function stmt_bind_assoc (&$stmt, &$out) {
    $data = mysqli_stmt_result_metadata($stmt);
    $fields = array();
    $out = array();

    $fields[0] = $stmt;
    $count = 1;

    while($field = mysqli_fetch_field($data)) {
        $fields[$count] = &$out[$field->name];
        $count++;
    }
    call_user_func_array(mysqli_stmt_bind_result, $fields);
}
Run Code Online (Sandbox Code Playgroud)

要使用它,只需调用它而不是调用bind_result:

$stmt->store_result();

$resultrow = array();
stmt_bind_assoc($stmt, $resultrow);

while($stmt->fetch())
{
    print_r($resultrow);
}
Run Code Online (Sandbox Code Playgroud)


Vin*_*vic 11

你可以这样写:

$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";
Run Code Online (Sandbox Code Playgroud)

但是要使用预处理语句,最好使用通用库,比如PDO

<?php
/* Execute a prepared statement by passing an array of values */
$sth = $dbh->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(); 
?>
Run Code Online (Sandbox Code Playgroud)

或者,使用mysqli

<?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);
?>
Run Code Online (Sandbox Code Playgroud)


Bil*_*win 8

我同意其他几个答案:

  • PHP ext/mysql不支持参数化SQL语句.
  • 查询参数在防止SQL注入问题时被认为更可靠.
  • mysql_real_escape_string() 如果你正确使用它也会很有效,但代码更详细.
  • 在某些版本中,国际字符集具有未正确转义的字符大小写,从而留下微妙的漏洞.使用查询参数可以避免这些情况.

您还应该注意,即使使用查询参数,仍然必须谨慎使用SQL注入,因为参数仅取代SQL查询中的文字值.如果您动态构建SQL查询并使用PHP变量作为表名,列名或SQL语法的任何其他部分,则mysql_real_escape_string()在这种情况下既不查询参数也不帮助.例如:

$query = "SELECT * FROM $the_table ORDER BY $some_column"; 
Run Code Online (Sandbox Code Playgroud)

关于表现:

  • 使用不同的参数值多次执行准备好的查询时,会带来性能优势.您可以避免解析和准备查询的开销.但是,您需要多次在同一PHP请求中多次执行相同的SQL查询?
  • 即使您可以利用这种性能优势,与通过有效使用操作码缓存或数据缓存等许多其他可以解决性能的方法相比,它通常只会略有改进.
  • 甚至有些情况下准备好的查询会损害性能.例如,在以下情况中,优化器不能假设它可以使用索引进行搜索,因为它必须假设参数值可能以通配符开头:

    SELECT * FROM mytable WHERE textfield LIKE ?
    
    Run Code Online (Sandbox Code Playgroud)