2013-11-21 116 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哦更快。這種虛構的差異是否會以任何特定的方式影響你? –