【发布时间】:2013-12-06 12:32:00
【问题描述】:
我正在尝试将旧网站转换为使用 mysqli 而不是 mysql。
用这部分代码遇到了一个绊脚石
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}
$theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($theValue) : mysqli_escape_string($theValue);
switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}
我不断收到错误
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given in
如果我添加这样的连接
$theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($test,$theValue) : mysqli_escape_string($test,$theValue);
得到错误
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null given
谁能告诉我我做错了什么
非常感谢
【问题讨论】:
-
错误信息准确地告诉您出了什么问题。如果您使用 RTM,您可以看到该函数有两个参数。你只提供一个。您丢失了连接。
-
你必须将mysqli句柄传递给函数php.net/manual/en/mysqli.real-escape-string.php
-
你不需要测试
function_exists("mysqli_real_escape_string")。如果 mysqli 存在,mysqli_real_escape_string也存在。就叫吧。 -
FFS,请学习使用准备好的语句。