Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Tests of the Enterprise Modules Database IO
- **********************************************************************************
- <?php
- /***************************************************************************/
- /* File: test_dio.php */
- /* Purpose: Tests the Enterprise Modules em_dio.php Database IO module. */
- /***************************************************************************/
- include('em_dio.php');
- function set_db() {
- $db_eng = 'mysql'; // Set DB engine
- $db_hst = 'localhost'; // Set DB engine
- $db_nam = 'accounts'; // Set DB name
- $db_prt = '3306'; // Set DB engine
- $db_pwd = 'nomened1497'; // Set DB PWD
- $db_tab = 'pg_menu'; // Set DB table
- $db_uid = 'ndavis'; // Set DB UID
- $db_lnk = new em_dio($db_nam, $db_hst, $db_prt, $db_uid, $db_pwd,
- $db_tab, $db_eng);
- $db_sql = "SELECT * FROM $db_nam.pg_menu WHERE `ptm_prc`='Accounts';";
- $result = $db_lnk->dbQuery($db_sql); // Run the Query
- return $result;
- } // end function set_db
- $res = set_db();
- $rw_cnt = 0;
- while ($row = mysql_fetch_assoc($res)) {
- echo "Rw=> $rw_cnt ";
- foreach ($row as $fld => $val) {
- echo "FLD=> $fld VAL=> $val ";
- } // end foreach
- echo " <br>";
- $rw_cnt++;
- }
- ?>
- **********************************************************************************
- <?php
- /***************************************************************************/
- /* File: em_dio.php */
- /* Purpose: The Enterprise Database IO module. */
- /***************************************************************************/
- class em_dio {
- var $db_col; // Data Table Column Name
- var $db_con; // Database Connection
- var $db_nam; // Database Name
- var $db_eng; // Database Engine
- var $db_hst; // Database Host
- var $db_prt; // Database Port
- var $db_pwd; // Database Password
- var $db_rct; // Data Query Returned Number of Rows
- var $db_res; // Data Query Returned Results Array
- var $db_row; // Data Query Returned Row
- var $db_sql; // Database SQL String
- var $db_tab; // Database Table
- var $db_uid; // Database User
- var $db_use; // Database Used
- public $auto_slashes;
- public $sql_error;
- public $linked;
- private $result = array(); // Results that are returned from the query
- public function __construct ($db_use, $db_hst, $db_prt, $db_uid,
- $db_pwd, $db_tab, $db_eng) {
- $this->db_con = false;
- $this->db_eng = $db_eng;
- $this->dbhost = $db_hst;
- $this->dbname = $db_use;
- $this->dbport = $db_prt;
- $this->dbpass = $db_pwd;
- $this->dbtabl = $db_tab;
- $this->dbuser = $db_uid;
- $this->linked = self::dbConnect();
- return $this->linked;
- } // end function __construct
- public function dbConnect() {
- /*********************************************************************/
- /* Function: dbConnect() */
- /* Purpose: Connects the database & reports errors. Only one */
- /* connection allowed. */
- /*********************************************************************/
- $link = $this->linked;
- if (!$link) {
- switch ($this->db_eng) {
- case 'mysql': // Process MySQL Engine
- $this->db_con = @mysql_connect($this->dbhost.":".$this->dbport,
- $this->dbuser, $this->dbpass) or die(mysql_error());
- if ($this->db_con) {
- $seldb = @mysql_select_db($this->dbname, $this->db_con)
- or die("Error=> ".mysql_error());
- if ($seldb) {
- $this->db_con = true;
- } else {
- $this->db_con = false;
- } // end if $db_con
- } // end if $conn
- break;
- case 'pgsql': // Process PostGres Engine
- $con_str = "host=$db_hst port=$db_prt user=$db_uid ".
- "password=$db_pwd dbname=$db_nam";
- $db_con = pg_connect($con_str)
- or die("Error: Failed to connect with CONN_STRING");
- if ($db_con) {
- $this->db_con = true;
- } else {
- $this->db_con = false;
- } // end if $db_con
- break;
- //case 'oracle': // Process Oracle Engine
- // break;
- //case 'mssql': // Process MS-SQL Engine
- // break;
- //case 'sybase': // Process SyBase Engine
- // break;
- } // end switch
- } // end if !$this->con
- if ($this->db_con === true) {
- return array($this->db_con, $this->db_use);
- } // end if $this->con
- } // end function dbConnect
- public function dbExec() {
- /***********************************************************************/
- /* Function: dbExec($db_sql) */
- /* Purpose: Runs the database query string & returns the results or */
- /* reports errors. */
- /***********************************************************************/
- if (!$linked) {
- $linked = self::dbConnect();
- } // end if !linked
- switch ($this->db_eng) {
- case 'mysql': // Process MySQL Engine
- $result = mysql_query($this->db_sql) or die(mysql_error());
- break;
- case 'pgsql': // Process PostGres Engine
- $result = pg_exec($this->db_sql) or die("Error: Query $this->db_sql Failed!");
- break;
- //case 'oracle': // Process Oracle Engine
- // break;
- //case 'mssql': // Process MS-SQL Engine
- // break;
- //case 'sybase': // Process SyBase Engine
- // break;
- } // end switch
- return $result;
- } // end function dbExec
- public function dbFetch($db_res,$db_row) {
- /***********************************************************************/
- /* Function: dbFetch() */
- /* Purpose: Runs the database query string & returns the results in */
- /* an array or reports errors. */
- /***********************************************************************/
- if (!$linked) {
- $linked = self::dbConnect();
- } // end if !linked
- switch ($this->db_eng) {
- case 'mysql': // Process MySQL Engine
- $result = mysql_result($db_res, $db_row) or die(mysql_error());
- break;
- case 'pgsql': // Process PostGres Engine
- $result = pg_fetch_row($db_res, $db_row) or die("Error: Fetch Failed!");
- break;
- //case 'oracle': // Process Oracle Engine
- // break;
- //case 'mssql': // Process MS-SQL Engine
- // break;
- //case 'sybase': // Process SyBase Engine
- // break;
- } // end switch
- return $result;
- } // end function dbFetch
- public function dbNRows($db_res) {
- /***********************************************************************/
- /* Function: dbRowRes() */
- /* Purpose: Checks the query result to find the number of rows. */
- /***********************************************************************/
- if (!$this->linked) {
- $linked = self::dbConnect();
- } // end if !linked
- switch ($this->db_eng) {
- case 'mysql': // Process MySQL Engine
- $result = mysql_num_rows($db_res) or die(mysql_error());
- break;
- case 'pgsql': // Process PostGres Engine
- $result = pg_numrows($db_res) or die("Error: Row Count Failed!");
- break;
- //case 'oracle': // Process Oracle Engine
- // break;
- //case 'mssql': // Process MS-SQL Engine
- // break;
- //case 'sybase': // Process SyBase Engine
- // break;
- } // end switch
- $this->db_rct = $result;
- return $this->db_rct;
- } // end function dbNRows
- public function dbQuery($db_sql) {
- /***********************************************************************/
- /* Function: dbQuery() */
- /* Purpose: Runs the database query string & returns the results or */
- /* reports errors. */
- /***********************************************************************/
- $link = $this->linked;
- //$link = self::$linked;
- if (!$link) {
- $this->linked = self::dbConnect();
- } // end if !linked
- switch ($this->db_eng) {
- case 'mysql': // Process MySQL Engine
- $result = mysql_query($db_sql) or die(mysql_error());
- break;
- case 'pgsql': // Process PostGres Engine
- $result = pg_query($this->db_sql);
- if ($result == false) {
- $result = pg_query($newquery) or die("Error: Query failed ".
- "with query string:<BR>$this->db_sql");
- } // end if $result
- break;
- //case 'oracle': // Process Oracle Engine
- // break;
- //case 'mssql': // Process MS-SQL Engine
- // break;
- //case 'sybase': // Process SyBase Engine
- // break;
- } // end switch
- if (self::dbNRows($result)==0) {
- return;
- } else {
- return $result;
- }
- } // end function dbQuery
- public function dbRes($db_res,$db_row,$db_col) {
- /***********************************************************************/
- /* Function: dbRes() */
- /* Purpose: Parses the results of a DB query by row and column to */
- /* get actual cell results. */
- /***********************************************************************/
- if (!$linked) {
- $linked = self::dbConnect();
- } // end if !linked
- switch ($this->db_eng) {
- case 'mysql': // Process MySQL Engine
- $result = mysql_result($db_res,$db_row,$db_col) or die(mysql_error());
- break;
- case 'pgsql': // Process PostGres Engine
- $result = pg_result($db_res,$db_row,$db_col) or die("Error: Cell ".
- "Parse Failed!");
- break;
- //case 'oracle': // Process Oracle Engine
- // break;
- //case 'mssql': // Process MS-SQL Engine
- // break;
- //case 'sybase': // Process SyBase Engine
- // break;
- } // end switch
- return $result;
- } // end function dbRes
- public function dbSet($name) {
- /***********************************************************************/
- /* Function: dbSet() */
- /* Purpose: Change to new DB and set all current results to null. */
- /***********************************************************************/
- if (!$linked) {
- $linked = self::dbConnect();
- } // end if !linked
- switch ($this->db_eng) {
- case 'mysql': // Process MySQL Engine
- if ($this->db_con) {
- if (@mysql_close()) {
- $this->db_con = false;
- $this->db_res = null;
- $this->db_nam = $name;
- $this->dbConnect();
- } // end if @mysql_close
- } // end if $this->db_con
- break;
- case 'pgsql': // Process PostGres Engine
- if ($this->db_con) {
- if (@pg_close()) {
- $this->db_con = false;
- $this->db_res = null;
- $this->db_nam = $name;
- $this->dbConnect();
- } // end if @pg_close
- } // end if $this->db_con
- break;
- //case 'oracle': // Process Oracle Engine
- // break;
- //case 'mssql': // Process MS-SQL Engine
- // break;
- //case 'sybase': // Process SyBase Engine
- // break;
- } // end switch
- return $result;
- } // end function dbNRows
- public function dbTabChk($table) {
- /***********************************************************************/
- /* Function: dbTabChk() */
- /* Purpose: Checks to see if table exists. */
- /***********************************************************************/
- if (!$linked) {
- $linked = self::dbConnect();
- } // end if !linked
- switch ($this->db_eng) {
- case 'mysql': // Process MySQL Engine
- $tabchkstr = 'SHOW TABLES FROM '.$this->db_name.' LIKE "'.$table.'"';
- $tablesInDb = @mysql_query($tabchkstr);
- if ($tablesInDb) {
- if (mysql_num_rows($tablesInDb)==1) {
- return true;
- } else {
- return false;
- } // end if mysql_num_rows
- } // end if $tablesInDb
- break;
- case 'pgsql': // Process PostGres Engine
- $tabchkstr = 'SELECT tablename FROM pg_tables LIKE "'.$table.'"';
- $tablesInDb = @pg_query($tabchkstr);
- if ($tablesInDb) {
- if (pg_numrows($tablesInDb)==1) {
- return true;
- } else {
- return false;
- } // end if mysql_num_rows
- } // end if $tablesInDb
- break;
- //case 'oracle': // Process Oracle Engine
- // break;
- //case 'mssql': // Process MS-SQL Engine
- // break;
- //case 'sybase': // Process SyBase Engine
- // break;
- } // end switch
- return $result;
- } // end function dbNRows
- public function dbInsert($table,$values,$rows = null) {
- /***********************************************************************/
- /* Function: dbInsert() */
- /* Purpose: Checks to see if table exists. */
- /***********************************************************************/
- if (!$linked) {
- $linked = self::dbConnect();
- } // end if !linked
- if ($this->dbTabChk($table)) {
- switch ($this->db_eng) {
- case 'mysql': // Process MySQL Engine
- $insert = 'INSERT INTO '.$table;
- if ($rows != null) {
- $insert .= ' ('.$rows.')';
- }
- for ($i = 0; $i < count($values); $i++) {
- if (is_string($values[$i]))
- $values[$i] = '"'.$values[$i].'"';
- }
- $values = implode(',',$values);
- $insert .= ' VALUES ('.$values.')';
- $ins = @mysql_query($insert);
- if($ins) {
- return true;
- } else {
- return false;
- }
- break;
- //case 'pgsql': // Process PostGres Engine
- // break;
- //case 'oracle': // Process Oracle Engine
- // break;
- //case 'mssql': // Process MS-SQL Engine
- // break;
- //case 'sybase': // Process SyBase Engine
- // break;
- } // end switch
- return $result;
- } // end if $this->dbTabChk
- } // end function dbNRows
- } // end class new_dio
- ?>
- **********************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement