php – 如何在mysqli中转义字符串

在旧的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();
}

解决方法:

您对该功能的使用不正确.

您必须使用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). "'";

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...