我的"real_escape_string“出了点问题,我需要一些帮助
login.php
<?php include('Connections/local.php'); ?>
<?php
function GetSQLValueString($sql) {
$sql = $mysqli->real_escape_string($sql);
return $sql;
}
?>local.php
<?php
$hostname_local = "xxx";
$database_local = "xxx";
$username_local = "xxx";
$password_local = "xxx";
$mysqli = new mysqli($hostname_local, $username_local, $password_local, $database_local);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
?>错误是
Undefined variable: mysqli我尝试了一些方法(比如在login.php中移动local.php的内容),但都不起作用
发布于 2015-03-04 11:20:36
你不能在你的函数中访问$mysqli。如果您愿意,可以在您的函数中添加global $mysqli;以使其可访问。或者,您可以将$mysqli作为参数传递。
发布于 2015-03-12 03:55:59
为什么不直接使用函数调用呢?
我的建议是,只要创建一个更简单(也更容易阅读)的函数"esc“,并在您需要转义任何与sql相关的内容时都可以使用它。
function esc($string, $mysqli = false) {
if (!$mysqli) global $mysqli;
return mysqli_real_escape_string($mysqli,$string);
}然后只需执行以下操作即可使用此命令:
$sql = esc($string); //if $mysqli is already set globally, and thus will be inherited by the function或
$sql = esc($string,$mysqli); //if $mysqli is to be passed into each func callhttps://stackoverflow.com/questions/28846099
复制相似问题