Possible Duplicate:
PHP Fatal error: Call to a member function Execute() on a non-object
I’m quite new to php and mysql so please bear with me. I wrote this code but I’m getting the following error when I attempt to delete a record from my database:
<?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
I guess that my code is lacking a function that would fetch the ‘id’ result from the form? I have no clue what I should write though.
You are mixing up PDO and mysqli syntax.
In PDO, you can use variables like
:idand bind them afterwards. In mysqli you have to use a question mark and you bind your variables before theexecutestatement and not by feeding it an array.In your case (taken from the manual, I normally use PDO):