Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Php Programming Code Examples

Php > Database Related Code Examples

MySQL MSSQL abstraction Layer

MySQL MSSQL abstraction Layer <?php /* This section should be in a file named DB.php */ class DB_base { // get server/instance function setinst($instance){ $this->instance = $instance; } // get username function setuser($user){ $this->user = $user; } // get password function setpass($pass){ $this->pass = $pass; } // get database name function dbname($dbname){ $this->dbname = $dbname; } // Use persistent connections? // Only call this if you want persistent connections function persist(){ $this->persist = 1; } } $file = join("", array("DB_", $dbtype, ".php")); require($file); ?> <?php /* This section should be in a file named DB_mysql.php */ class DB extends DB_base { // Connect or pconnect. function connect(){ if($this->persist){ $this->conn = mysql_pconnect($this->instance, $this->user, $this->pass); } else { $this->conn = mysql_connect($this->instance, $this->user, $this->pass); } mysql_select_db($this->dbname, $this->conn); return($this->conn); } // close function close(){ if($this->persist) { $ret = 1; } else { $ret = mysql_close($this->conn); } return($ret); } // query function query($query){ $this->result = mysql_query($query); return($this->result); } // numrows function numrows(){ $this->numrows = mysql_num_rows($this->result); return($this->numrows); } // affected rows function affrows(){ $this->affrows = mysql_affected_rows($this->result); return($this->affrows); } // seek function seek($row){ $seek = mysql_data_seek($this->result, $row); return($seek); } // fetch object function fobject(){ $object = mysql_fetch_object($this->result); return($object); } // fetch array function farray(){ $array = mysql_fetch_array($this->result); return($array); } // free function free(){ $free = mysql_free_result($this->result); return($free); } } ?> /* example usage <?php $dbtype = "mysql"; require("DB.php"); $db = new DB; $db->setinst("dbserver"); $db->setuser("dbusername"); $db->setpass("dbpassword"); $db->dbname("dbname"); $db->persist(); $conn = $db->connect(); $db->query("SELECT id,narf FROM foo WHERE id > 4"); while($r = $db->fobject()){ echo "$r->id: $r->narf<br>\n"; } ?> */