db_dbname  = $database;
        }
        if ($server   !== null) {
            $this->db_host    = $server;
        }
        if ($username !== null) {
            $this->db_user    = $username;
        }
        if ($password !== null) {
            $this->db_pass    = $password;
        }
        if ($charset  !== null) {
            $this->db_charset = $charset;
        }
        if (strlen($this->db_host) > 0 &&
            strlen($this->db_user) > 0) {
            if ($connect && (!$this->isConnected())) {
                $this->open();
            }
        }
    }
    /**
	 * Destructor: Closes the connection to the database
	 *
	 */
    public function __destruct()
    {
        $this->close();
    }
    /**
	 * Automatically does an INSERT or UPDATE depending if an existing record
	 * exists in a table
	 *
	 * @param string $tableName The name of the table
	 * @param array $valuesArray An associative array containing the column
	 *                            names as keys and values as data. The values
	 *                            must be SQL ready (i.e. quotes around
	 *                            strings, formatted dates, ect)
	 * @param array $whereArray An associative array containing the column
	 *                           names as keys and values as data. The values
	 *                           must be SQL ready (i.e. quotes around strings,
	 *                           formatted dates, ect).
	 * @return boolean Returns TRUE on success or false on error
	 */
    public function autoInsertUpdate($tableName, $valuesArray, $whereArray)
    {
        $this->resetError();
        $this->selectRows($tableName, $whereArray);
        if (! $this->error()) {
            if ($this->hasRecords()) {
                return $this->updateRows($tableName, $valuesArray, $whereArray);
            } else {
                return $this->insertRow($tableName, $valuesArray);
            }
        } else {
            return false;
        }
    }
    /**
	 * Returns true if the internal pointer is at the beginning of the records
	 *
	 * @return boolean TRUE if at the first row or false if not
	 */
    public function beginningOfSeek()
    {
        $this->resetError();
        if ($this->isConnected()) {
            if ($this->active_row < 1) {
                return true;
            } else {
                return false;
            }
        } else {
            $this->setError("No connection");
            return false;
        }
    }
    /**
	 * [STATIC] Builds a comma delimited list of columns for use with SQL
	 *
	 * @param array $valuesArray An array containing the column names.
	 * @param boolean $addQuotes (Optional) TRUE to add quotes
	 * @param boolean $showAlias (Optional) TRUE to show column alias
	 * @return string Returns the SQL column list
	 */
    private static function buildSQLColumns($columns, $addQuotes = true, $showAlias = true)
    {
        if ($addQuotes) {
            $quote = "`";
        } else {
            $quote = "";
        }
        switch (gettype($columns)) {
            case "array":
                $sql = "";
                foreach ($columns as $key => $value) {
                    // Build the columns
                    if (strlen($sql) == 0) {
                        $sql = $quote . $value . $quote;
                    } else {
                        $sql .= ", " . $quote . $value . $quote;
                    }
                    if ($showAlias && is_string($key) && (! empty($key))) {
                        $sql .= ' AS "' . $key . '"';
                    }
                }
                return $sql;
                break;
            case "string":
                return $quote . $columns . $quote;
                break;
            default:
                return false;
                break;
        }
    }
    /**
	 * [STATIC] Builds a SQL DELETE statement
	 *
	 * @param string $tableName The name of the table
	 * @param array $whereArray (Optional) An associative array containing the
	 *                           column names as keys and values as data. The
	 *                           values must be SQL ready (i.e. quotes around
	 *                           strings, formatted dates, ect). If not specified
	 *                           then all values in the table are deleted.
	 * @return string Returns the SQL DELETE statement
	 */
    public static function buildSQLDelete($tableName, $whereArray = null)
    {
        $sql = "DELETE FROM `" . $tableName . "`";
        if (! is_null($whereArray)) {
            $sql .= self::buildSQLWhereClause($whereArray);
        }
        return $sql;
    }
    /**
	 * [STATIC] Builds a SQL INSERT statement
	 *
	 * @param string $tableName The name of the table
	 * @param array $valuesArray An associative array containing the column
	 *                            names as keys and values as data. The values
	 *                            must be SQL ready (i.e. quotes around
	 *                            strings, formatted dates, ect)
	 * @return string Returns a SQL INSERT statement
	 */
    public static function buildSQLInsert($tableName, $valuesArray)
    {
        $columns = self::buildSQLColumns(array_keys($valuesArray));
        $values  = self::buildSQLColumns($valuesArray, false, false);
        $sql = "INSERT INTO `" . $tableName .
               "` (" . $columns . ") VALUES (" . $values . ")";
        return $sql;
    }
    /**
	 * Builds a simple SQL SELECT statement
	 *
	 * @param string $tableName The name of the table
	 * @param array $whereArray (Optional) An associative array containing the
	 *                          column names as keys and values as data. The
	 *                          values must be SQL ready (i.e. quotes around
	 *                          strings, formatted dates, ect)
	 * @param array/string $columns (Optional) The column or list of columns to select
	 * @param array/string $sortColumns (Optional) Column or list of columns to sort by
	 * @param boolean $sortAscending (Optional) TRUE for ascending; false for descending
	 *                               This only works if $sortColumns are specified
	 * @param integer/string $limit (Optional) The limit of rows to return
	 * @return string Returns a SQL SELECT statement
	 */
    public static function buildSQLSelect($tableName, $whereArray = null, $columns = null, $sortColumns = null, $sortAscending = true, $limit = null)
    {
        if (! is_null($columns)) {
            $sql = self::buildSQLColumns($columns);
        } else {
            $sql = "*";
        }
        $sql = "SELECT " . $sql . " FROM `" . $tableName . "`";
        if (is_array($whereArray)) {
            $sql .= self::buildSQLWhereClause($whereArray);
        }
        if (! is_null($sortColumns)) {
            $sql .= " ORDER BY " .
                    self::buildSQLColumns($sortColumns, true, false) .
                    " " . ($sortAscending ? "ASC" : "DESC");
        }
        if (! is_null($limit)) {
            $sql .= " LIMIT " . $limit;
        }
        return $sql;
    }
    /**
	 * [STATIC] Builds a SQL UPDATE statement
	 *
	 * @param string $tableName The name of the table
	 * @param array $valuesArray An associative array containing the column
	 *                            names as keys and values as data. The values
	 *                            must be SQL ready (i.e. quotes around
	 *                            strings, formatted dates, ect)
	 * @param array $whereArray (Optional) An associative array containing the
	 *                           column names as keys and values as data. The
	 *                           values must be SQL ready (i.e. quotes around
	 *                           strings, formatted dates, ect). If not specified
	 *                           then all values in the table are updated.
	 * @return string Returns a SQL UPDATE statement
	 */
    public static function buildSQLUpdate($tableName, $valuesArray, $whereArray = null)
    {
        $sql = "";
        foreach ($valuesArray as $key => $value) {
            if (strlen($sql) == 0) {
                $sql = "`" . $key . "` = " . $value;
            } else {
                $sql .= ", `" . $key . "` = " . $value;
            }
        }
        $sql = "UPDATE `" . $tableName . "` SET " . $sql;
        if (is_array($whereArray)) {
            $sql .= self::buildSQLWhereClause($whereArray);
        }
        return $sql;
    }
    /**
	 * [STATIC] Builds a SQL WHERE clause from an array.
	 * If a key is specified, the key is used at the field name and the value
	 * as a comparison. If a key is not used, the value is used as the clause.
	 *
	 * @param array $whereArray An associative array containing the column
	 *                           names as keys and values as data. The values
	 *                           must be SQL ready (i.e. quotes around
	 *                           strings, formatted dates, ect)
	 * @return string Returns a string containing the SQL WHERE clause
	 */
    public static function buildSQLWhereClause($whereArray)
    {
        $where = "";
        foreach ($whereArray as $key => $value) {
            if (strlen($where) == 0) {
                if (is_null($value)) {
                    $where = " WHERE `" . $key . "` IS NULL";
                } elseif (is_string($key)) {
                    $where = " WHERE `" . $key . "` = " . $value;
                } else {
                    $where = " WHERE " . $value;
                }
            } else {
                if (is_null($value)) {
                    $where = " AND `" . $key . "` IS NULL";
                } elseif (is_string($key)) {
                    $where .= " AND `" . $key . "` = " . $value;
                } else {
                    $where .= " AND " . $value;
                }
            }
        }
        return $where;
    }
    /**
	 * Close current MySQL connection
	 *
	 * @return object Returns TRUE on success or false on error
	 */
    public function close()
    {
        $this->resetError();
        $this->active_row = -1;
        $success = $this->release();
        if ($success) {
            $success = @mysqli_close($this->mysql_link);
            if (! $success) {
                $this->setError();
            } else {
                unset($this->last_sql);
                unset($this->last_result);
                unset($this->mysql_link);
            }
        }
        return $success;
    }
    /**
	 * Deletes rows in a table based on a WHERE filter
	 * (can be just one or many rows based on the filter)
	 *
	 * @param string $tableName The name of the table
	 * @param array $whereArray (Optional) An associative array containing the
	 *                          column names as keys and values as data. The
	 *                          values must be SQL ready (i.e. quotes around
	 *                          strings, formatted dates, ect). If not specified
	 *                          then all values in the table are deleted.
	 * @return boolean Returns TRUE on success or false on error
	 */
    public function deleteRows($tableName, $whereArray = null)
    {
        $this->resetError();
        if (! $this->isConnected()) {
            $this->setError("No connection");
            return false;
        } else {
            $sql = self::buildSQLDelete($tableName, $whereArray);
            // Execute the UPDATE
            if (! $this->query($sql)) {
                return false;
            } else {
                return true;
            }
        }
    }
    /**
	 * Returns true if the internal pointer is at the end of the records
	 *
	 * @return boolean TRUE if at the last row or false if not
	 */
    public function endOfSeek()
    {
        $this->resetError();
        if ($this->isConnected()) {
            if ($this->active_row >= ($this->RowCount())) {
                return true;
            } else {
                return false;
            }
        } else {
            $this->setError("No connection");
            return false;
        }
    }
    /**
	 * Returns the last MySQL error as text
	 *
	 * @return string Error text from last known error
	 */
    public function error()
    {
        $error = $this->error_desc;
        if (empty($error)) {
            if ($this->error_number != 0) {
                $error = "Unknown Error (#" . $this->error_number . ")";
            } else {
                $error = false;
            }
        } else {
            if ($this->error_number > 0) {
                $error .= " (#" . $this->error_number . ")";
            }
        }
        return $error;
    }
    /**
	 * Returns the last MySQL error as a number
	 *
	 * @return integer Error number from last known error
	 */
    public function errorNumber()
    {
        if (strlen($this->error_desc) > 0) {
            if ($this->error_number != 0) {
                return $this->error_number;
            } else {
                return -1;
            }
        } else {
            return $this->error_number;
        }
    }
    /**
	 * [STATIC] Converts any value of any datatype into boolean (true or false)
	 *
	 * @param mixed $value Value to analyze for TRUE or false
	 * @return boolean Returns TRUE or false
	 */
    public static function getBooleanValue($value)
    {
        if (gettype($value) == "boolean") {
            if ($value == true) {
                return true;
            } else {
                return false;
            }
        } elseif (is_numeric($value)) {
            if ($value > 0) {
                return true;
            } else {
                return false;
            }
        } else {
            $cleaned = strtoupper(trim($value));
            if ($cleaned == "ON") {
                return true;
            } elseif ($cleaned == "SELECTED" || $cleaned == "CHECKED") {
                return true;
            } elseif ($cleaned == "YES" || $cleaned == "Y") {
                return true;
            } elseif ($cleaned == "TRUE" || $cleaned == "T") {
                return true;
            } else {
                return false;
            }
        }
    }
    /**
	 * Returns the comments for fields in a table into an
	 * array or NULL if the table has not got any fields
	 *
	 * @param string $table Table name
	 * @return array An array that contains the column comments
	 */
    public function getColumnComments($table)
    {
        $this->resetError();
        $records = mysqli_query($this->mysql_link, "SHOW FULL COLUMNS FROM " . $table);
        if (! $records) {
            $this->setError();
            return false;
        } else {
            // Get the column names
            $columnNames = $this->getColumnNames($table);
            if ($this->error()) {
                return false;
            } else {
                $index = 0;
                // Fetchs the array to be returned (column 8 is field comment):
                while ($array_data = mysqli_fetch_array($records)) {
                    $columns[$index] = $array_data[8];
                    $columns[$columnNames[$index++]] = $array_data[8];
                }
                return $columns;
            }
        }
    }
    /**
	 * This function returns the number of columns or returns false on error
	 *
	 * @param string $table (Optional) If a table name is not specified, the
	 *                      column count is returned from the last query
	 * @return integer The total count of columns
	 */
    public function getColumnCount($table = "")
    {
        $this->resetError();
        if (empty($table)) {
            $result = mysqli_field_count($this->mysql_link);
            if (! $result) {
                $this->setError();
            }
        } else {
            $records = mysqli_query($this->mysql_link, "SELECT * FROM " . $table . " LIMIT 1");
            if (! $records) {
                $this->setError();
                $result = false;
            } else {
                $result = mysqli_field_count($this->mysql_link);
                $success = @mysqli_free_result($records);
                if (! $success) {
                    $this->setError();
                    $result = false;
                }
            }
        }
        return $result;
    }
    /**
	 * This function returns the data type for a specified column. If
	 * the column does not exists or no records exist, it returns false
	 *
	 * @param string $column Column name or number (first column is 0)
	 * @param string $table (Optional) If a table name is not specified, the
	 *                      last returned records are used
	 * @return string MySQL data (field) type
	 */
    public function getColumnDataType($column, $table = "")
    {
        $this->resetError();
        if (empty($table)) {
            if ($this->RowCount() > 0) {
                if (is_numeric($column)) {
                    $field = mysqli_fetch_field_direct($this->last_result, $column);
                    return $field->type;
                } else {
                    $field = mysqli_fetch_field_direct($this->last_result, $this->getColumnID($column));
                    return $field->type;
                }
            } else {
                return false;
            }
        } else {
            if (is_numeric($column)) {
                $column = $this->getColumnName($column, $table);
            }
            $result = mysqli_query($this->mysql_link, "SELECT " . $column . " FROM " . $table . " LIMIT 1");
            if (mysqli_field_count($this->mysql_link) > 0) {
                $field = mysqli_fetch_field_direct($result, 0);
                return $field->type;
            } else {
                $this->setError("The specified column or table does not exist, or no data was returned", -1);
                return false;
            }
        }
    }
    /**
	 * This function returns the position of a column
	 *
	 * @param string $column Column name
	 * @param string $table (Optional) If a table name is not specified, the
	 *                      last returned records are used.
	 * @return integer Column ID
	 */
    public function getColumnID($column, $table = "")
    {
        $this->resetError();
        $columnNames = $this->getColumnNames($table);
        if (! $columnNames) {
            return false;
        } else {
            $index = 0;
            $found = false;
            foreach ($columnNames as $columnName) {
                if ($columnName == $column) {
                    $found = true;
                    break;
                }
                $index++;
            }
            if ($found) {
                return $index;
            } else {
                $this->setError("Column name not found", -1);
                return false;
            }
        }
    }
   /**
	 * This function returns the field length or returns false on error
	 *
	 * @param string $column Column name
	 * @param string $table (Optional) If a table name is not specified, the
	 *                      last returned records are used.
	 * @return integer Field length
	 */
    public function getColumnLength($column, $table = "")
    {
        $this->resetError();
        if (empty($table)) {
            if (is_numeric($column)) {
                $columnID = $column;
            } else {
                $columnID = $this->getColumnID($column);
            }
            if (! $columnID) {
                return false;
            } else {
                $field = mysqli_fetch_field_direct($this->last_result, $columnID);
                if (! $field) {
                    $this->setError();
                    return false;
                } else {
                    return $field->length;
                }
            }
        } else {
            $records = mysqli_query($this->mysql_link, "SELECT " . $column . " FROM " . $table . " LIMIT 1");
            if (! $records) {
                $this->setError();
                return false;
            }
            $field = mysqli_fetch_field_direct($records, 0);
            if (! $field) {
                $this->setError();
                return false;
            } else {
                return $field->length;
            }
        }
    }
   /**
	 * This function returns the name for a specified column number. If
	 * the index does not exists or no records exist, it returns false
	 *
	 * @param string $columnID Column position (0 is the first column)
	 * @param string $table (Optional) If a table name is not specified, the
	 *                      last returned records are used.
	 * @return integer Field Length
	 */
    public function getColumnName($columnID, $table = "")
    {
        $this->resetError();
        $result = false;
        if (empty($table)) {
            if ($this->RowCount() > 0) {
                $field = mysqli_fetch_field_direct($this->last_result, $columnID);
                if (! $field) {
                    $result = false;
                    $this->setError();
                } else {
                    $result = $field->name;
                }
            } else {
                $result = false;
            }
        } else {
            $records = mysqli_query($this->mysql_link, "SELECT * FROM " . $table . " LIMIT 1");
            if (! $records) {
                $this->setError();
                $result = false;
            } else {
                if (mysqli_field_count($this->mysql_link) > 0) {
                    $field = mysqli_fetch_field_direct($records, $columnID);
                    if (! $field) {
                        $result = false;
                        $this->setError();
                    } else {
                        $result = $field->name;
                    }
                } else {
                    $result = false;
                }
            }
        }
        return $result;
    }
    /**
	 * Returns the field names in a table or query in an array
	 *
	 * @param string $table (Optional) If a table name is not specified, the
	 *                      last returned records are used
	 * @return array An array that contains the column names
	 */
    public function getColumnNames($table = "")
    {
        $this->resetError();
        if (empty($table)) {
            $columnCount = mysqli_field_count($this->mysql_link);
            if (! $columnCount) {
                $this->setError();
                $columns = false;
            } else {
                for ($column = 0; $column < $columnCount; $column++) {
                    $field = mysqli_fetch_field_direct($this->last_result, $column);
                    $columns[] = $field->name;
                }
            }
        } else {
            $result = mysqli_query($this->mysql_link, "SHOW COLUMNS FROM " . $table);
            if (! $result) {
                $this->setError();
                $columns = false;
            } else {
                while ($array_data = mysqli_fetch_array($result)) {
                    $columns[] = $array_data[0];
                }
            }
        }
        // Returns the array
        return $columns;
    }
    /**
	 * This function returns the last query as an HTML table
	 *
	 * @param boolean $showCount (Optional) TRUE if you want to show the row count,
	 *                           false if you do not want to show the count
	 * @param string $styleTable (Optional) Style information for the table
	 * @param string $styleHeader (Optional) Style information for the header row
	 * @param string $styleData (Optional) Style information for the cells
	 * @return string HTML containing a table with all records listed
	 */
    public function getHTML($showCount = true, $styleTable = null, $styleHeader = null, $styleData = null)
    {
        if ($styleTable === null) {
            $tb = "border-collapse:collapse;empty-cells:show";
        } else {
            $tb = $styleTable;
        }
        if ($styleHeader === null) {
            $th = "border-width:1px;border-style:solid;background-color:navy;color:white";
        } else {
            $th = $styleHeader;
        }
        if ($styleData === null) {
            $td = "border-width:1px;border-style:solid";
        } else {
            $td = $styleData;
        }
        if ($this->last_result) {
            if ($this->RowCount() > 0) {
                $html = "";
                if ($showCount) {
                    $html = "Record Count: " . $this->RowCount() . "
\n";
                }
                $html .= "
| " . htmlspecialchars($key) . " | \n"; } $html .= "\t
| " . htmlspecialchars($value) . " | \n"; } $html .= "\t