Tuesday 2 June 2015

MySQL class

<?php
class sql {
 public static $db;
 public static $login = array(
  'host' => 'localhost',
  'user' => 'user',
  'pass' => 'pass',
  'db' => 'name'
 );
 public static $rs;

 function __construct() {
  sql::connect();
 }
 
 public function connect() {
  # Initialize Connection
  sql::$db = new mysqli(sql::$login['host'], sql::$login['user'], sql::$login['pass'], sql::$login['db']);
  # Check Connection
  if (mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
  }
 }
 
 public function query($query) {
  # Perform Query
  sql::$rs = sql::$db->query($query);
 }
}
?>
 
 

Usage

Edit the variables at the top of the class.

Init and connect with $sql = new sql();

Perform a query with sql::query($query). Result is stored in sql::$rs
 

0 comments:

Post a Comment