2012-08-25 243 views
3

Possible Duplicate:
PHP Fatal error: Call to a member function Execute() on a non-objectPHP錯誤:調用一個成員函數的execute()非對象

我很新的PHP和MySQL,所以請多多包涵上。我寫了這個代碼,但我得到以下錯誤,當我試圖從我的數據庫中刪除一條記錄:

<?php 
require_once 'login_rankings.php'; 
$mysqli = new mysqli($db_hostname,$db_username,$db_password,$db_database); 
if ($mysqli->connect_errno) { 
echo "Failed to connect to database: " . $mysqli->connect_errno . ": " . 
$mysqli->connect_error; 
} 

$query = "SELECT * FROM nfl"; 
$stmt = $mysqli->prepare($query); 
$stmt->execute(); 
$stmt->store_result(); 
printf("Number of rows: %d.\n", $stmt->num_rows); echo "<br /><br /><br />"; 

if (isset($_POST['delete']) && isset($_POST['id'])) 
{ 
// $id = get_post('id'); 
$query = "DELETE FROM nfl WHERE id= :id LIMIT 1"; 



try{ 
$stmt = $mysqli->prepare($query); 
$stmt->execute(array(':id' => $_POST['id'])); 
}catch(Exception $e){ 
error_log($e->getMessage() . '\r\n',3, errors.log); 
} 

/* not working either 
    $stmt = $mysqli->prepare($query); 
    $stmt->bind_param("id", $id); 
    $stmt->execute(); */ 
} 


$rows = $stmt->num_rows; 
$res = $mysqli->query($query); 

for ($j = 0; $j < $rows; ++$j) 
{ 
$row = $res->fetch_assoc(); 
require_once 'abbrs.php'; 

echo <<<_END 
<pre> 
Last name: $row[lastname] 
First name: $row[firstname] 
Team: $row[team] 
Position: $row[position] 
Number: $row[number] 
Age: $row[age] 
Id: $row[id]</pre> 
<form action="ranking_nfl.php" method="post"> 
<input type="hidden" name="delete" value="yes" /> 
<input type="hidden" name="id" value="$row[id]" /> 
<input type="submit" value="DELETE" /> 
</form> 
_END; 

} 


$stmt->close(); 

$mysqli->close(); 
?> 

Fatal error: Call to a member function execute() on a non-object in C:\web\project2\ranking_nfl.php on line 24

我想我的代碼是缺乏將取「身份證」結果的函數從形式?我不知道我應該寫什麼。

回答

3

您正在混合使用PDO和mysqli語法。

在PDO中,您可以使用像:id這樣的變量並在之後進行綁定。在mysqli中,您必須使用問號,並在execute語句之前綁定變量,而不是爲其提供數組。

在你的情況(從manual拍攝,我通常使用PDO):

$query = "DELETE FROM nfl WHERE id= ? LIMIT 1"; 

try{ 
    $stmt = $mysqli->prepare($query); 
    $stmt->bind_param('i', $_POST['id']); 
    $stmt->execute(); 
+1

現在好了,這是有道理的。我從某人那裏得到了一小部分使用PDO的代碼,並且我沒有意識到這是PDO而不是mysqli。非常感謝,它現在正在工作! – Pawel

相關問題