Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 清理一些SQL查询_Php_Sql - Fatal编程技术网

Php 清理一些SQL查询

Php 清理一些SQL查询,php,sql,Php,Sql,最简单、最有效的消毒方法是什么: $q = "SELECT * FROM `admin` " ."WHERE `username`=' ".$_POST["username"]."' " ."AND `passcode`=' ".$_POST["password"]."' " 此外,我正在学习PHP,因此如果您能提供解释、提示、建议或更多方法来清理内容,以防止SQL注入,我将非常感谢这些方法我喜欢使用$mysqli准备好的语句-以下是PHP站点的一个示例: 说明(请参见底部使用代码的示例

最简单、最有效的消毒方法是什么:

$q = "SELECT * FROM `admin` " 
."WHERE `username`=' ".$_POST["username"]."' " 
."AND `passcode`=' ".$_POST["password"]."' " 

此外,我正在学习PHP,因此如果您能提供解释、提示、建议或更多方法来清理内容,以防止SQL注入,我将非常感谢这些方法

我喜欢使用$mysqli
准备好的语句
-以下是PHP站点的一个示例:

说明(请参见底部使用代码的示例):

您可以使用
标记替换查询中的变量,然后在以后的时间将变量绑定到中

$city = "Amersfoort";

/* create a prepared statement */
if ($stmt = $mysqli->prepare("SELECT District FROM City WHERE Name=?")) {

    /* bind parameters for markers */
    $stmt->bind_param("s", $city);

    /* execute query */
    $stmt->execute();

    /* bind result variables */
    $stmt->bind_result($district);

    /* fetch value */
    $stmt->fetch();

    printf("%s is in district %s\n", $city, $district);

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();

使用代码的示例:

$q = "SELECT * FROM `admin` " 
."WHERE `username`= ? AND passcode = ?";


/* create a prepared statement */
    if ($stmt = $mysqli->prepare($q)) {

        /* bind parameters for markers */
        $stmt->bind_param("ss", $_POST['username'], $_POST['password']);

        /* execute query */
        $stmt->execute();

        /* bind result variables */
        $stmt->bind_result($district);

        /* fetch value */
        $stmt->fetch();  // This can also be while($stmt->fetch()){ Code here }

        printf("%s is in district %s\n", $city, $district);

        /* close statement */
        $stmt->close();
    }

    /* close connection */
    $mysqli->close();

如果你真的要知道,它会是这样的:

$q = "SELECT * FROM `admin` " 
."WHERE `username`=' ".mysql_real_escape_string($_POST["username"])."' " 
."AND `passcode`=' ".mysql_real_escape_string($_POST["password"])."' "

但正如其他人所说,您应该使用最有可能的PDO或mysqli,因为所有mysql_uu命令都已弃用。

使用PDO或mysqli,并查看为它们准备的语句。它们都有很好的文件化选项。使用MySQL LealRealEffeIsShutoButt来清除查询,你应该考虑散列和加密你的密码,而不是把它们存储在纯文本中。我相信你试图让LoLo吸吮的示例代码。我也会使用PDO MySqLi并不是真的那么好。“示例使用您的代码”在顶部抛出$q=行,其他什么都不做哦,是的,看看我,我是这样一个巨魔,不管这个主题是死的,无论如何,继续下去没有意义。