2014-03-31 46 views
0

我正在尋找對此錯誤的解釋/修復。我正在使用以下腳本。這是我第一次使用multi_query:錯誤使用multi_query「調用一個非對象的成員函數multi_query()」

 $storyidr=$_POST['storyidr']; 
$mysqli = mysqli_connect($dbhost,$dbusername,$dbpasswd,$database_name) or die ("Couldn't connect to server."); 
if (mysqli_connect_errno($mysqli)) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
$sql = "INSERT INTO ratings (storyidr, rank, entry_date) VALUES ('$_POST[storyidr]','$_POST[value]',now());"; 
$sql .= "SELECT AVG(rank) AS avrank from ratings WHERE storyidr = $storyidr"; 
if($mysqli->multi_query($sql)) 
{ 
if ($result = $mysqli->store_result()) 
{ 
$data = mysqli_fetch_assoc($result); 
$avrank = $data['avrank']; 
     if(!$result) 
     { 
       $arr = array ('status'=>'fail'); 
       echo json_encode($arr); 
     } 
     else 
     { 

       echo json_encode($avrank); 
     } 
       exit; 
} 
} 
+0

旁註 變化:由於它你的代碼是打開[** SQL注入**](http://stackoverflow.com/q/60174/) –

回答

2

沒有定義的變量$mysqli,你是在分配給$con,從

$con = mysqli_connect($dbhost,$dbusername,$dbpasswd,$database_name) or die ("Couldn't connect to server."); 

$mysqli = mysqli_connect($dbhost,$dbusername,$dbpasswd,$database_name) or die ("Couldn't connect to server."); 
+1

另外,'if(mysqli_connect_errno($ con))'給'if(mysqli_co nnect_errno($ mysqli))' –

+0

這樣做擺脫錯誤,謝謝!腳本雖然不工作。你不會在這裏發現任何其他問題嗎?我在編輯?發佈當前代碼。當使用觸發aJax時,會顯示一個空值(無數據) – rhill45

相關問題