MySql class

Code that the staff think is particularly good will be copied here.
Locked
conradk
Posts: 117
Joined: Tue Jul 05, 2011 10:41 pm

MySql class

Post by conradk »

jacek wrote:This is an archive post, any discussion should take place here http://betterphp.co.uk/board/viewtopic.php?f=5&t=493
A simple MySql class. A lot of these things are already in the BetterPHP library, or similar things. I just tried building this myself as sort of a practice:
/* MySql class
 *
 * @created : 8.7.2011
 * @function : simplify MySql queries
 */
class MySQL {
   private $conn;
   private $db;
   private $username;
   private $password;
   private $host;
   private $port;

   public function __construct($host, $user, $pw, $db, $port = '3306') {
      $this->username = $user;
      $this->password = $pw;
      $this->host     = $host;
      $this->port     = $port;
      $this->db       = $db;
      
      $this->conn = mysql_connect($this->host . ':' . $this->port, $this->username, $this->password);
      mysql_select_db($this->db, $this->conn);
   }
   public function createDb($name) {
      mysql_query("CREATE DATABASE {$name}",$this->conn);
   }
   public function freeResult($result) {
      mysql_free_result($result);
   }
   public function close() {
      mysql_close($this->conn);
   }
   public function query($query) {
      mysql_query($query, $this->conn);
   }
   public function selectDb($name) {
      mysql_select_db($name, $this->conn);
   }
   public function esc($str) {
      $var = mysql_real_escape_string($str);
      return $var;
   }
}
Locked