這裏的是我的代碼:PHP /庫MySQLi - 致命錯誤:調用一個成員函數mysqli_query()非對象
require "../include/functions.php";
error_reporting(E_ALL);
ini_set('display_errors', '1');
ConnectWithMySQLiDatabase();
$Cat = addslashes($_POST["Category"]);
$v = $conn->mysqli_query($conn,"SELECT * FROM `categories` WHERE `id`=$Cat");
$vrowi = mysqli_fetch_array($v, MYSQLI_ASSOC);
$url = $conn->real_escape_string($vrowi['Link']);
這是我在functions.php
:
function ConnectWithMySQLiDatabase() {
global $dbhost, $dbuser, $dbpass, $database, $HTTP_SERVER_VARS;
$conn = new mysqli($dbhost, $dbuser, $dbpass, $database);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$conn->set_charset("utf8");
global $conn;
}
變量$dbhost, $dbuser, $dbpass, $database,
設置正確。
當我嘗試執行此mysqli_query我收到以下錯誤:
<b>Fatal error</b>: Call to a member function mysqli_query() on a non-object in <b>/fetch_category_products.php</b> on line <b>19</b><br />
第19行是:
$v = $conn->mysqli_query($conn,"SELECT * FROM `categories` WHERE `id`=$Cat");
能否請你告訴我哪裏是我的錯誤,我該如何解決?
在此先感謝!
** **危險:你是* *容易受到[SQL注入攻擊](http://bobby-tables.com/)**,你需要[抗辯](http://stackoverflow.com/questions/60174/best-way-to-prevent- sql-injection-in-php)自己從。 ('addslashes'對此不安全,即使'addslashes'的文檔也這樣說) – Quentin
我用什麼來取代它? 'mysqli_real_escape_string'? –
@TonyStark請考慮爲您的數據庫工作使用PDO。 – TwoStraws