gpt4 book ai didi

php - 使用这个自定义的 MySQLi 转义 PHP 函数有什么问题吗?

转载 作者:行者123 更新时间:2023-11-29 01:20:18 25 4
gpt4 key购买 nike

我写了一个基本的包装函数来使用 MySQLi 转义字符串。使用这个有什么问题吗?是不是比原来的好?有用吗?

该函数有两个参数,$conn,它是 MySQLi 连接,和 &$var,它是你想要转义的字符串。

function escapestr($conn, &$var){
$var = $conn->real_escape_string($var);
return $var;
}

用法:

$conn = mysqli_connect("localhost", "username", "password", "my_favourite_db");
$userInput = $_GET["input"]; // value: this is my "inputted" string
$userInput = escapestr($conn, $userInput); // value: this is my \"inputted\" string

或者,它可以直接更新变量。

$conn = mysqli_connect("localhost", "username", "password", "my_favourite_db");
$userInput = $_GET["input"]; // value: this is my "inputted" string
escapestr($conn, $userInput); // value: this is my \"inputted\" string

最佳答案

Is there anything wrong with using this?

是的。你应该使用 prepared statements (又名参数化语句或参数化查询)而不是重新发明轮子。

Is is better than the original?

您所做的只是将一个函数包装在一个函数中。虽然有时这很有用,但这不是其中之一。

Is it useful?

没有

关于php - 使用这个自定义的 MySQLi 转义 PHP 函数有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39082828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com