link = mysql_connect($MYSQL_HOST, $MYSQL_USER, $MYSQL_PASS); if (!mysql_select_db($MYSQL_DATABASE, $this->link)) { print "Failure: " . mysql_error() . "
\n"; } /* Make sure to do a string comparison, just in case we have a * non-numeric id field */ if ($id != '0' && $this->tableName != '' && $this->keyField != '') { /* If we have a defined id value given then pre-populate the data. */ $query = "SELECT * FROM " . $this-> tableName . " WHERE " . $this->keyField . " = $id"; $result = mysql_query($query, $this->link); if (!$result) { array_push($this->errors, new Error("DB Error:" . mysql_error(), __LINE__, mysql_errno)); } /* Fetch the results */ $this->data = mysql_fetch_assoc($result); } else { $this->data = Array(); } } /* Return the number of errors that have occured */ function errorCount() { return(count($this->errors)); } /* Get the nth error from the error array */ function getError($idex = 0) { return($this->errors[$idex]); } function clearError() { $this->errors = array(); } /* Update the database with the information from this row. */ function update() { /* Create the query to update the database */ $query = "REPLACE " . $this->table . " SET "; $separator = ''; foreach($this->fields as $fieldName) { if ($this->data[$fieldName] != null) { $value = verifyField($this->fields[$fieldName], $this->data[$fieldName]); if ($value == '' && $this->data[$fieldName] != '') { array_push($this->errors, new Error("Field: $fieldName is not a value " . $this->fields[$fieldName] . " type.")); } else { $query .= "$separator$fieldName = '" . $this->data[$fieldName] . "'"; $separator = ", "; } } else { /* Skip nullable fields and use the database default value to fill in the missing values */ if ($this->legallyNull[$fieldName] != 1 || $fieldName == $keyField) { array_push($this->errors, new Error("Field: $fieldName is null", __LINE__, 1)); } } } /* Don't execute the query if we have any errors at all */ if ($this->errorCount > 0) { return(false); } /* Execute the query */ if (!mysql_query($query, $this->link)) { array_push($this->errors, new Error("DB Error: " . mysql_error(), __LINE__, mysql_errno())); return(false); } return(true); } } ?>