- Class Database {
- var $host = NULL;
- var $username = NULL;
- var $password = NULL;
- var $databaseName = NULL;
- var $link = NULL;
- var $queries = NULL;
- var $errors = NULL;
- var $databaseExtras = NULL;
- function __construct ($host, $username, $password, $database) {
- $this->database ($host, $username, $password, $database);
- }
- function Database ($host, $username, $password, $database) {
- /* $this->database = Array (
- "Host" = $host,
- "Username" = $username,
- "Password" = $password,
- "Database" = $database,
- "Link" and "" ",
- "Queries" = = Array (),
- "Errors" = = Array ()
- );*/
- $this->host = $host;
- $this->username = $username;
- $this->password = $password;
- $this->databasename = $database;
- $this->link = "";
- $this->queries = Array ();
- $this->errors = Array ();
- $this->databaseextras = new StdClass;
- $this->link = mysql_connect ($this->host, $this->username, $this->password) or Die ("Could does connect to Database ");
- mysql_select_db ($this->databasename);
- }
- function Justquery ($sql) {
- $this->queries[] = $sql;
- Return mysql_query ($sql, $this->link);
- }
- function Loadresult ($sql) {
- if (! ( $cur = $this->justquery ($sql))) {
- return null;
- }
- $ret = null;
- if ($row = Mysql_fetch_row ($cur)) {
- $ret = $row [0];
- }
- Mysql_free_result ($cur);
- return $ret;
- }
- function Loadfirstrow ($sql) {
- if (! ( $cur = $this->justquery ($sql))) {
- return null;
- }
- $ret = null;
- if ($row = Mysql_fetch_object ($cur)) {
- $ret = $row;
- }
- Mysql_free_result ($cur);
- return $ret;
- }
- function Insertid () {
- Return mysql_insert_id ($this->link);
- }
- function query ($sql, $key = "", $returns = True, $batch = False) {
- $result = Array ();
- Switch ($batch) {
- Default
- Case true:
- foreach ($sql as $index = = $query) {
- $this->queries[] = $query;
- $answer = mysql_query ($query, $this->link);
- if (! $answer) {
- $this->errors[] = mysql_error ($this->link);
- }
- else {
- if ($returns! = False) {
- if (mysql_num_rows ($answer) > 0) {
- while ($row = Mysql_fetch_object ($answer)) {
- if ($key! = "") {
- $result [$index] [$row-$key] = $row;
- }
- else {
- $result [$index] = $row;
- }
- }
- } else {}
- } else {}
- }
- }
- Break
- Case false:
- $this->queries[] = $sql;
- $answer = mysql_query ($sql, $this->link);
- if (! $answer) {
- $this->errors[] = mysql_error ($this->link);
- $result = false;
- }
- else {
- if ($returns! = False) {
- if (mysql_num_rows ($answer) > 0) {
- while ($row = Mysql_fetch_object ($answer)) {
- if ($key! = "") {
- $result [$row, $key] = $row;
- }
- else {
- $result [] = $row;
- }
- }
- } else {}
- }
- else {
- $result = true;
- }
- }
- Break
- }
- return $result;
- }
- function Loadobject ($sql, & $object) {
- if ($object! = null) {
- if (! ( $cur = $this->justquery ($sql))) {
- return false;
- } else {}
- if ($array = Mysql_fetch_assoc ($cur)) {
- Mysql_free_result ($cur);
- $this->bindarraytoobject ($array, $object);
- return true;
- }
- else {
- return false;
- }
- }
- else {
- if ($cur = $this->justquery ($sql)) {
- if ($object = Mysql_fetch_object ($cur)) {
- Mysql_free_result ($cur);
- return true;
- }
- else {
- $object = null;
- return false;
- }
- }
- else {
- return false;
- }
- }
- }
- function Bindarraytoobject ($array, & $obj) {
- if (!is_array ($array) | |!is_object ($obj)) {
- return (false);
- }
- foreach (Get_object_vars ($obj) as $k = = $v) {
- if (substr ($k, 0, 1) = = ' _ ') {
- $ak = $k;
- if (Isset ($array [$ak])) {
- $obj $k = $array [$ak];
- }
- }
- }
- return true;
- }
- function Formatcsvcell ($data) {
- $useQuotes = false;
- $quotable = Array (
- "\" "=" \ "\" ",
- "," = ",",
- "\ n" = "\ n"
- );
- foreach ($quotable as $char = = $repl) {
- if (eregi ($char, $data)) {
- $useQuotes = true;
- } else {}
- }
- if ($useQuotes = = True) {
- foreach ($quotable as $char = = $repl) {
- $data = Str_replace ($char, $repl, $data);
- }
- $data = "\" ". $data. "\"";
- }
- else {
- }
- return $data;
- }
- }
- ?>
Copy Code
|