0

I'm having some issues with this MYSQL staement for updating a value [foodie_count=foodie_count-8] in one table [users] while having the value [food.id=?] from another table [food] and both tables are linked [users.id=food.userid]:

require_once('./includes/connection.inc.php');
$stmt = $db->prepare("UPDATE users SET foodie_count=foodie_count-8 FROM food INNER JOIN users ON users.id=food.userid WHERE food.id=?");
$stmt->bind_param('i',$delete_event);
$stmt->execute();

The statement is not proper and throwing me a fatal error of a call to a member function prepare() on a non-object.

I looked at some other posts, but don't specifically see where I error in writing the statement. Thanks.

rocket_boomerang_19
  • 519
  • 1
  • 3
  • 19
  • 1
    Looks like your syntax is wrong: http://stackoverflow.com/questions/1262786/mysql-update-query-based-on-select-query – Jordan Kaye Oct 29 '12 at 21:35
  • Fatal error calling prepare() on a non-object means `$db` does not contain what it is supposed to. Your database connection failed, or was overwritten somewhere. – Michael Berkowski Oct 29 '12 at 21:43

1 Answers1

2

MySQL UPDATE JOIN syntax is a bit... different.

This should be the same query with the syntax you need for MySQL;

UPDATE users
JOIN food 
  ON users.id=food.userid 
SET foodie_count=foodie_count-8 
WHERE food.id=?

Simple SQLfiddle testing ground

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294