Sigleton DB connection tripping on itself
| From: | Vandegrift, Ken | Date: | Tue, 18 Jul 2006 17:41:20 +0000 |
| Subject: | Sigleton DB connection tripping on itself | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-42223@lists.php.net to get a copy of this message | ||
Good Day List,
I have a singleton DB connection that I am trying to use throughout my
application but It keeps tripping on itself.
My main question is: If I am in the middle of a transaction can I
prepare multiple statements eand execute them or will this cause an
error?
Here is a code example that tries to reset a users password:
$login = SOME_LOGIN;
$sql = "SELECT " . USER_TABLE_ID . " FROM " . USER_TABLE . " " .
"WHERE " . USER_TABLE_LOGIN . " = :login;";
try {
$this->db->beginTransaction();
$stmt = $this->db->prepare($sql);
$stmt->execute(array(':login' => $login));
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
// MAKE SURE THERE IS ONLY ONE USER WITH THIS LOGIN
if (count($rs) != 1) {
$this->db->rollBack();
$stmt = null;
return false;
}
// ONLY ONE USER RETURNED - UPDATE PASSWORD
if ($password = $this->generatePassword()) {
$sql = "UPDATE " . USER_TABLE . " " .
"SET " . USER_TABLE_PWD . " = '";
if ($this->sha1) {
$sql .= sha1($password);
}else {
$sql .= $password;
}
$sql .= "' " .
"WHERE " . USER_TABLE_ID . " = :userid;";
$stmt = $this->db->prepare($sql);
$stmt->execute(array(':userid' => $rs[0][USER_TABLE_ID]));
$this->db->commit();
$stmt = null;
return $password;
}
// COULD NOT GENERATE NEW PASSWORD
else {
$this->db->rollBack();
$stmt = null;
return false;
}
}catch (PDOException $e) {
$this->db->rollBack();
$stmt = null;
return false;
}
Ken Vandegrift
kvandegrift@sharis.com <mailto:kvandegrift@sharis.com>
Web Administrator
Sharis Mgmt. Corp