<?php //------------------------------------------------------------------------------------------ ※database () constructor, database initial parameters ※select () query ※getrows () returns the total number of records in the query ※insert () Insert Record ※update () Update ※delete () Delete ※halt () Interrupt and display error message * * //------------------------------------------------------------------------------------------ Define ("DatabaseType", "1"); Defines the database type: 1 for mysql;2 SQL server;3 for Oracle;4 for ODBC Define ("SERVER", "localhost"); Host name or IP address of the database server Define ("DATABASE", "dbname"); The name of the database to connect to Define ("USER", "TableName"); User name used to connect to the database Define ("PASSWORD", "paswd"); Password used to connect to the database Class Database { var $dbLink; Connection handle var $result; Query handle var $insId; Insert () successfully returns the value of the Auto_increment column var $rows; Returns an array of data var $numRows; Number of returned data var $dbHost, $dbUser, $userPassword, $database; var $dbType = DatabaseType; var $msgFlag = "Yes"; Yes:show the MYSQL message; No:die by show "halted." function Database ($dbHost = SERVER, $dbUser = USER, $userPassword = PASSWORD, $database = database) { Switch ($this->dbtype) { Case 1: $this->dblink = @mysql_pconnect ($dbHost, $dbUser, $userPassword); Or Die ("Can ' t Connect to Remote host!"); @mysql_select_db ($database, $this->dblink); Or Die ("Can ' t Connect to Remote host!"); Break Case 2: Break } return true; } /* Sql:select () return to False no result * * function Select ($table, $columns, $condition = 1) { $sql = "Select $columns from $table where $condition"; $this->result = @mysql_query ($sql, $this->dblink); unset ($this->rows); if ($this->result) { $i = 0; if (!) ( $this->rows = Array ("$i" => @mysql_fetch_array ($this->result))) return false; if ($this->numrows = @mysql_num_rows ($this->result)) = = 0) return false; while ($tempRows = @mysql_fetch_array ($this->result)) { Array_push ($this->rows, $tempRows); } } else { $this->halt ($sql); return false; } return true; } /* Sql:getrows () returns the total number of records for the query * * function GetRows ($table, $condition = 1) { $sql = " Select COUNT (1) as Count from $table where $condition; $this->result = @mysql_query ($sql, $this->dblink); if ($this->result) { $temp = @mysql_fetch_array ($this->result); $this->numrows = $temp [Count]; } else { $this->halt ($sql); return false; } return $this->numrows; } /* Sql:insert () * * function Insert ($table, $columns, $values) { $sql = "INSERT into $table ($columns) VALUES ($values)"; $this->result = @mysql_query ($sql, $this->dblink); if ($this->result) $this->insid = @mysql_insert_id ($this->dblink); else { $this->halt ($sql); return false; } return true; } /* Sql:update () * * function Update ($table, $setings, $condition) { $sql = "Update $table set $setings where $condition"; $this->result = @mysql_query ($sql, $this->dblink); if ($this->result) $this->numrows = @mysql_affected_rows ($this->result); else { $this->halt ($sql); return false; } return true; } * Sql:delete * * function Delete ($table, $condition) { $sql = "Delete from $table where $condition"; $this->result = @mysql_query ($sql, $this->dblink); if ($this->result) $this->numrows = @mysql_affected_rows ($this->result); else { $this->halt ($sql); return false; } return true; } /* Halt (): Error message * * function Halt ($msg) { if ($this->msgflag = = " Yes "{ printf (" <b>database Query Error:</b>%s<br>n ", $msg); printf ("<b>mysql error:</b>%s <br>n ", mysql_error ()); }else echo "<meta Http-equiv=refresh content= ' 0; Url=.. /include/error.htm ' > '; A custom error prompt file return false; } } Switch ($db->dbtype) { Case 1: @mysql_close (); Break Case 2: Break } $db = new Database (); ?> |