2013-11-21 123 views
0

我想转换旧网站使用mysqli而不是mysql。从MYSQL转换到MYSQLI

撞了一下一个stumberling块与这部分代码

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); 

连接GET错误

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 

有人能告诉我什么,我做错了

非常感谢

+3

错误消息告诉您确切* *什么是错的。如果你使用RTM,你可以看到这个函数有两个参数。你只提供一个。你错过了你的连接。 –

+0

您必须将mysqli句柄传递给函数http://php.net/manual/en/mysqli.real-escape-string.php – Nicolai

+0

您不需要测试'function_exists(“mysqli_real_escape_string”)''。如果mysqli存在,'mysqli_real_escape_string'也是如此。只需调用它。 – deceze

回答

0

mysqli_real_escape_string必须也通过了数据库连接。所以你需要提供的功能,然后执行:

mysqli_escape_string($connection, $theValue);

http://php.net/manual/en/mysqli.real-escape-string.php

此外,它会告诉你,你的$test变量是null - 再次,它期待的活动数据库连接你用myqli_connect()创建。您可能需要将其作为参数传递给该函数。

+3

你不应该使用或链接到[w3schools](http://www.w3fools.com)。这不是一个可靠的信息来源,我们不想鼓励它的使用。更不用说php.net是PHP函数的权威资源。 –

+0

请阅读整个问题。 – deceze

5

获取。摆脱。的。这个。整个。功能。

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

然而,这些机制都相当不便,因此,更好地迈向PDO prepared statements.

+1

这是正确的答案,开始使用PDO并使用准备好的语句。这将改善代码的可读性和安全性。 – StigM

+0

但是mysqli比PDO – Darline

+2

@Darline哦更快。这种虚构的差异是否会以任何特定的方式影响你? –