Simon Carlson
Simon Carlson

Reputation: 1989

Parameterized update SQL query using PHP

I have a table where two entries has id = 10. My SQL update query looks like this:

$q = "UPDATE `table_name` SET `col_name` = 'value' WHERE `id` = ?";
if($con-prepare($q)){
    $stmt->bind_param("i","10");
    $stmt->execute;
    $stmt->fetch;
    $stmt->close();
}

The query will not run, where is the error in it? And also, when it does run, will it update ALL entries where id = 10 or just the first one?

Upvotes: 0

Views: 261

Answers (1)

bwoebi
bwoebi

Reputation: 23787

$stmt->execute();
$stmt->fetch();

Do not forget the () to show that's a method call else PHP interprets it as a property access.

And write: $stmt = $con->prepare() instead of $con-prepare() (or do you want to subtract the return of the function call to prepare() from $con?)

It'll update all the entries with id=10

Upvotes: 1

Related Questions