} return $rows; } public function update($keyedArray) { $sql = "update {$this->_tableName} set "; foreach ($keyedArray as $column => $value) { $updates[] = "{$column} = '{$value}' "; } $sql .= implode(",", $updates); $sql .= "where {$this->_primaryKey}='{$keyedArray[$this->_primaryKey]}'"; mysql_query($sql, $this->__connection); } } class userDAO extends baseDAO { protected $_tableName = "userTable"; protected $_primaryKey = "id"; public function getUserByFirstName($name) { $result = $this->fetch($name, 'firstName'); return $result; } } $user = new userDAO(); $id = 1; $userInfo = $user->fetch($id); $updates = array('id' => 1, 'firstName' => 'arlon'); $user->update($updates); $all = $user->getUserByFirstName('arlon');
$sql = "update {$this->_tableName} set "; $updates = array(); foreach ($keyedArray as $column => $value) { $updates[] = "{$column} = '{$value}'"; } $sql .= implode(',', $updates); $sql .= "where {$this->_primaryKey} = '{$keyedArray[$this->_primaryKey]}'"; mysql_query($sql, $this->__connection); } } class userDAO extends baseDAO { protected $_tableName = 'userTable'; protected $_primaryKey = 'id'; public function getUserByFirstName($name) { $result = $this->fetch($name, 'firstName'); return $result; } } define('DB_USER', 'root'); define('DB_PASS', 'root'); define('DB_HOST', 'localhost'); define('DB_DATABASE', 'test'); $user = new userDAO(); $userDetailsArray = $user->fetch(1); print_r($userDetailsArray); $updates = array('id' => 1, 'firstName' => 'aaron'); $user->update($updates); $allAarons = $user->getUserByFirstName('aaron'); print_r($allAarons);