我已經看過這麼多人有這個相同的問題,但沒有解決方案是相關/他們沒有工作。完全失去了與mysqli_real_escape_string
首先,這裏是我的簡單的代碼讓一切變得簡單:
<?php
$mysqli = new mysqli("localhost", "root", "", "pembsweb");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
function mysqli_secure($string) {
$string = htmlspecialchars($string);
$string = trim($string);
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
$string = $mysqli->real_escape_string($string);
return $string;
}
echo mysqli_secure("lets\\test//this");
?>
這將導致錯誤:
Fatal error: Call to a member function real_escape_string() on a non-object on line 13
我感覺真的很愚蠢,激怒現在..誰能幫我?
編輯:我試圖從功能刪除所有其他行,因此,所有它做的是mysqli的世外桃源,仍然沒有。我也試圖通過$string = new stdClass();
在功能上的第一行定義$字符串,但仍然沒有任何結果。
這是一個範圍問題;你的'$ mysqli'對象不在你的函數中。 – andrewsi
它不在範圍內? :/它保持開放,當然我不需要在使用資源的每個函數中重新定義一個mysqli連接? – Chuckun
不,但您需要將它傳遞給函數。處理這個問題的更好方法是使用一個類,其中每個函數都可以使用連接作爲對象屬性。 – deceze