Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class MySQL {
- private $hostname;
- private $username;
- private $password;
- private $database;
- private $link;
- private $result;
- private $config = false;
- public function __construct() {
- }
- public function close() {
- mysql_close($this->link);
- }
- public function connect($hostname = null, $username = null, $password = null) {
- if (func_num_args() == 0) {
- if ($this->config) {
- $hostname = $this->hostname;
- $username = $this->username;
- $password = $this->password;
- } else {
- die("MySQL::connect() failed -- the config is missing.");
- }
- }
- $this->link = mysql_connect($hostname, $username, $password);
- if (!$this->link) {
- die("MySQL::connect() failed -- the connection couldn't be established: ".mysql_error());
- }
- }
- public function fetchRow() {
- return mysql_fetch_row($this->result);
- }
- public function loadConfig($config = "config.php") {
- require_once $config;
- if (!defined("HOSTNAME") || !defined("USERNAME") || !defined("PASSWORD") || !defined("DATABASE")) {
- die("MySQL::loadConfig() failed -- the required configuration constants couldn't be found.");
- }
- $this->hostname = HOSTNAME;
- $this->username = USERNAME;
- $this->password = PASSWORD;
- $this->database = DATABASE;
- $this->config = true;
- }
- public function query($query) {
- if (func_num_args() > 1) {
- for ($i = 1; $i < func_num_args(); $i++) {
- $args[] = mysql_real_escape_string(func_get_arg($i));
- }
- $query = vsprintf($query, $args);
- }
- $this->result = mysql_query($query);
- if (!$this->result) {
- die("MySQL::query -- invalid query: " . mysql_error());
- }
- }
- public function rows() {
- return mysql_num_rows($this->result);
- }
- public function selectDatabase($database = null) {
- if (func_num_args() == 0) {
- if ($this->config) {
- $database = $this->database;
- } else {
- die("MySQL::selectDatabase() failed -- the config is missing.");
- }
- }
- $selected = mysql_select_db($database, $this->link);
- if (!$selected) {
- die ("MySQL::selectDatabase() failed -- the database couldn't be selected: ".mysql_error());
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement