Is it acceptable to always use the same variable (for example, $ stmt) for PDO requests?

advertisements

I always use the same variable for my PDO query statements, for example:

// Query 1
$stmt = $db->prepare("UPDATE table_1 SET name=? WHERE somthing=?");
$stmt->bindValue(1, $name, PDO::PARAM_STR);
$stmt->bindValue(2, $something, PDO::PARAM_STR);
$stmt->execute();

// Query 2 (right after the above)
$stmt = $db->prepare("UPDATE table_2 SET another_column=? WHERE id=?");
$stmt->bindValue(1, $another_column, PDO::PARAM_STR);
$stmt->bindValue(2, $id, PDO::PARAM_INT);
$stmt->execute();

I know it is ok for the first lines ($stmt = $db->...), my doubt is about binding values. For example if I forget to bind something in first query will my query use the next binding in second query (or vice versa)? or everything is reset after execute()?

Which one is a better practice?

  1. Using same variable to avoid mistakes (e.g. Always $stmt)
  2. Using different variables

Using different variables makes it easier to debug, however I do this occasionally because it is easier to type-hint a single statement.

my doubt is about binding values.

each db->prepare() returns a brand new \PDOStatement so there is no issue about binding values.