在旧的mysql()代码中,为了转义字符串,我这样做了:
t.TeacherUsername = '".mysql_real_escape_string($teacherusername)."'
我正在将我的代码更改为mysqli,但我想知道并确保安全,在mysqli中转义字符串是这样的:
t.TeacherUsername = '".mysqli_real_escape_string($teacherusername)."'
还要连接到mysqli数据库,如下所示:
$username="xxx";
$password="xxx";
$database="xxx";
mysqli_connect('localhost',$username,$password);
mysqli_select_db($database) or die( "Unable to select database");
我所做的就是将mysql改为mysqli,这是正确的吗?
更新:
现在这是使用mysqli连接数据库的正确方法:
$username="xxx";
$password="xxx";
$database="mobile_app";
$mysqli = new mysqli("localhost", $username, $password, $database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
答案 0 :(得分:4)
您对该功能的使用不正确。
您必须使用mysqli_connect
返回的mysqli链接资源作为mysqli_real_escape_string
的第一个参数。
示例:
$username="xxx";
$password="xxx";
$database="xxx";
$my = mysqli_connect('localhost',$username,$password);
mysqli_select_db($my, $database) or die( "Unable to select database");
$t->TeacherUsername = "'" . mysqli_real_escape_string($my, $teacherusername). "'";