0

Fatal error: Call to a member function execute() on boolean in ---- on line 18

I keep getting this error. What I want to happen is for when you click the delete button on a post it will delete it. This is line 18

$result->execute(array(':id' => $_GET['delpost']));

PHP

if (isset($_GET['delpost'])) { 

    $result = mysqli_query( $connection,'DELETE FROM blog WHERE id = :id') ;
    $result->execute(array(':id' => $_GET['delpost']));

    header('Location: blog.php?action=deleted');
    exit;
}
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
Jane
  • 3
  • 1
  • 1
  • 4
  • Possible duplicate of [mysqli\_fetch\_array()/mysqli\_fetch\_assoc()/mysqli\_fetch\_row() expects parameter 1 to be resource or mysqli\_result, boolean given](http://stackoverflow.com/questions/2973202/mysqli-fetch-array-mysqli-fetch-assoc-mysqli-fetch-row-expects-parameter-1) The error message is slightly different, but for the same reason and the same underlying cause. `mysqli_query()` is failing and returning a value of `false`. Check `mysql_error()`. – David Dec 09 '16 at 01:57
  • 2
    Are you using `mysqli` or `PDO`? Placeholders beginning with `:` are only for PDO. – Barmar Dec 09 '16 at 01:58
  • @David The problem is that he's calling `mysqi_query` when he should be calling `mysqli_prepare`. – Barmar Dec 09 '16 at 02:02
  • @Barmar: I'm seeing that now too, ya. Of course, `mysql_error()` would also reveal the fact that the query is being executed without using the parameter, so still useful for his own debugging :) – David Dec 09 '16 at 02:03
  • @David It would probably say that there's a syntax error near `:id`, but I doubt that would be very helpful to him in figuring out what's wrong. – Barmar Dec 09 '16 at 02:12
  • If you don't already have good understanding of how MySQL works, its error messages can be very inscrutable. – Barmar Dec 09 '16 at 02:14

2 Answers2

11

You need to use prepare() to create a prepared statement, not query. You're also mixing PDO and mysqli, that won't work.

If you're using PDO, it should be:

$result = $connection->prepare('DELETE FROM blog WHERE id = :id');
$result->execute(array(':id' => $_GET['delpost']));

If you're using mysqli, it should be:

$result = $connection->prepare('DELETE FROM blog WHERE id = ?');
$result->bind_param('i', $_GET['delpost']);
$result->execute();
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

If nothing works check how you provided your values for table.. write is like this:

insert into `table_name`(`column_name`) values('values')

Use single quote for values

Hope it helps.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shabbir Ahmed
  • 228
  • 4
  • 7