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

Edd*_*art 2 php mysql mysqli

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

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

function escapestr($conn, &$var){
    $var = $conn->real_escape_string($var);
    return $var;
}
Run Code Online (Sandbox Code Playgroud)

用法:

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

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

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

Joh*_*nde 6

使用它有什么问题吗?

是.您应该使用预准备语句(也就是参数化语句或参数化查询),而不是重新发明轮子.

是比原来更好?

你所做的就是在函数中包装一个函数.虽然有时会有用,但这不是其中之一.

它有用吗?

没有