Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
FILE HOSTS PREMIUM ACCOUNT
ALL FILE HOST PREMIUM ACCOUNTS
Zynga Slingo Trainer v5.12
iTunes Gift Card Generator V3.1 2012
Diablo 3 GOLD Coins FREE
Working PS3 Jailbreak 3.65 And 3.66
ExtaBit Premium Accounts and Cookies
Steam Wallet Hack - Money Adder & Hack v3
Empires & Allies Hack Cheat Trainer v5.4.1
Eve Onnline 60 Days Time Card Generator v2
Popular
XBOX POINTS GENERATOR - MICROSOFT POINTS GENERATOR v1.2012
11 may 2012 premium uploading accounts 100% working
Free Microsoft Points
Free Microsoft Points - Microsoft Points Generator - Xbox Live Codes 2012
Car Town Free Blue Points Hack
Free CarTown Blue Points Generator and CarTown Templates
Better way to get content via jQuery $.get()
Free Microsoft Points
Simple Days Purger
Sharecash Downloader Bypass Surveys New 05/2012
Pastable version of
PHP5 Database Clas
<pre class='prettyprint' language='php'><?php class DB { //connection variables protected $dbhost = 'localhost'; protected $dbuser = 'root'; protected $dbpass = ''; protected $dbname = 'dbname'; //normal global resources protected $db = null; protected static $instance = null; /** * Singleton pattern for instantiating the DB class */ public static function GetInstance() { if(!self::$instance) { self::$instance = new DB; } return self::$instance; } protected function __construct() { if(($db = @mysql_connect($this->dbhost, $this->dbuser, $this->dbpass)) === false) { trigger_error('There was an error connecting to the server.'); exit; } if((@mysql_select_db($this->dbname, $db)) === false) { trigger_error('There was an error selecting the database.'); exit; } $this->db = $db; } /** * simple method for sanitizing SQL query strings. */ public function Clean($string) { if(get_magic_quotes_gpc()) { $string = stripslashes($string); } return mysql_real_escape_string($string); } /** * DB class method call to the Query child class. * SELECT queries only * Returns an object. */ public function Query($statement) { return new Query($statement, $this->db); } /** * DB class method call to the Execute child class. * INSERT/UPDATE/DELETE queries only * Returns number of rows affected. */ public function Execute($statement) { return new Execute($statement, $this->db); } /** * Used to get the last inserted record */ public function InsertID() { return mysql_insert_id($this->link); } /** * The transactional functions are giving me more of a headache than * I sould care to have. The general idea behind what I want to do * with them is be able to pass an array of SQL queries into one * main method call, which in turn calls its child methods. * * The issue I have with this is that there are instances where I * dont want to base a Commit / Rollback on whether or not the * queries succeed or fail, but perhaps the time of day, or the * privelage level of the user running the queries, etc. */ public function Transaction($queryArray) { /** * this method call will return a new instance of the * Transaction CLass. Operations are documented within * the Transaction Class. */ return new Transaction($queryArray, $this->db); } public function __destruct() { @mysql_close($this->db); } } /** * These are separate classes for the reason that I would like * to be able to edit them with as little distruption to the * flow of the parent class as possible. */ /** * Query child class. Used for running SELECT queries. * Called exclusively by the DB parent class in the Query method */ class Query { protected $result; public function __construct($statement, $link) { if(($this->result = mysql_query($statement, $link)) === false) { trigger_error('There was an error in processing the Query method: '. mysql_error()); exit; } } public function Fetch($class = null) { return @mysql_fetch_object($this->result); } public function __destruct() { @mysql_free_result($this->result); } } /** * Execute child class. Used for running INSERT/UPDATE/DELETE queries. * Called exclusively by the DB parent class in the Execute method */ class Execute { public function __construct($statement, $link) { if((@mysql_query($statement, $link)) == false) { trigger_error('There was an error processing the Execute method: '. mysql_error()); exit; } return @mysql_affected_rows($link); } } /** * Transaction child class. Used for transactional queries. * Called exclusively by the DB parent class in the Transaction method */ class Transaction { public function __construct($queryArray, $link) { /** * This is not working at all the way I want it to. * * Essentially what it is doing, is running the first * query, then basing the Commit / Rollback on the * outcome of that single query. That's no good. * * I can see _why_ its not working, but that doesn't * really help me to fix it. * * $hold is set to 1 as its starting value, and as * the loop progresses it either changes that or * doesnt, depending on the outcome. What it seems * to be doing is running it once, and deciding * "Hey! the query worked!, lets commit changes!". */ $hold = 1; $this->Begin(); foreach($queryArray as $query) { @mysql_query($query, $link); if(@mysql_affected_rows($link) == 0) { $hold = 0; } } if($hold == 0) { $this->Rollback(); return false; } else { $this->Commit(); return true; } } private function Begin() { @mysql_query('BEGIN', $link); } private function Commit() { @mysql_query('COMMIT', $link); } private function Rollback() { @mysql_query('ROLLBACK', $link); } } ?></pre> <a href="http://www.refactormycode.com/codes/391-php5-database-clas" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>