从MYSQL转换为MYSQLI

Dar*_*ine 0 php mysql mysqli

我试图将旧网站转换为使用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;
}
}
Run Code Online (Sandbox Code Playgroud)

我一直在收到错误

Warning:  mysqli_real_escape_string() expects exactly 2 parameters, 1 given in 

Warning:  mysqli_real_escape_string() expects exactly 2 parameters, 1 given in
Run Code Online (Sandbox Code Playgroud)

如果我添加这样的连接

$theValue = function_exists("mysqli_real_escape_string") ? mysqli_real_escape_string($test,$theValue) : mysqli_escape_string($test,$theValue);
Run Code Online (Sandbox Code Playgroud)

得到错误

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
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我我做错了什么

非常感谢

You*_*nse 6

得到.摆脱.的.这个.整个.功能.

它不应该与mysqli一起使用.因为mysqli有自己的机制,必须使用它.

但是,这些机制非常不方便,因此,更好地转向PDO 准备的声明.

  • @Darline哦,拜托.这种想象中的差异会以任何特定的方式影响你吗? (3认同)
  • 这是正确的答案,开始使用PDO并使用准备好的语句。这将提高代码的可读性和安全性。 (2认同)