Php “问题”;真正的“逃逸字符串”;

Php “问题”;真正的“逃逸字符串”;,php,Php,我的“真正的逃生绳”遇到了一些麻烦,我需要一些帮助 login.php <?php include('Connections/local.php'); ?> <?php function GetSQLValueString($sql) { $sql = $mysqli->real_escape_string($sql); return $sql; } ?> 我尝试了一些方法(比如在login.php中移动local.php的内容),但没有任何效果您

我的“真正的逃生绳”遇到了一些麻烦,我需要一些帮助

login.php

<?php include('Connections/local.php'); ?>
<?php
function GetSQLValueString($sql) {
    $sql = $mysqli->real_escape_string($sql);
    return $sql;
}
?>

我尝试了一些方法(比如在login.php中移动local.php的内容),但没有任何效果

您无法在函数中访问$mysqli。
如果需要,添加
global$mysqli以使其可访问。或者,您可以将$mysqli作为参数传递。

为什么不直接使用函数调用呢

我的建议是,只需创建一个更简单(且更易于阅读)的函数“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


这是一个范围问题:
$mysqli
超出范围,只需在参数中包含连接即可
 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 call