trashbin/include/mysql.class.php

90 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2014-07-07 08:54:45 +00:00
<?php
class MySQL
{
2014-07-07 08:54:45 +00:00
var $db_host;
var $db_user;
var $db_pwd;
var $db_name;
var $queries = 0;
var $connections = 0;
var $db;
2014-07-07 08:54:45 +00:00
function set($db_host, $db_user, $db_pwd, $db_name)
{
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pwd = $db_pwd;
$this->db_name = $db_name;
}
2014-07-07 08:54:45 +00:00
function connect()
{
$this->db = new mysqli($this->db_host, $this->db_user, $this->db_pwd, $this->db_name);
$this->db = new mysqli($this->db_host, $this->db_user, $this->db_pwd, $this->db_name);
if ($this->db->connect_errno > 0) {
die('Unable to connect to database [' . $this->db->connect_error . ']');
}
$this->connections++;
}
2014-07-07 08:54:45 +00:00
function query($sql)
{
$db = $this->db;
if (!isset($this->db)) {
$this->connect();
}
if (!$result = $db->query($sql)) {
die('There was an error running the query [' . $this->db->error . ']');
2014-07-07 08:54:45 +00:00
}
// used for other functions
$this->queries++;
return $result;
}
2014-07-07 08:54:45 +00:00
function fetch_array($result)
{
2014-07-07 08:54:45 +00:00
// create an array called $row
$row = mysqli_fetch_array($result);
2014-07-07 08:54:45 +00:00
// return the array $row or false if none found
return $row;
}
function escape($string)
{
return mysqli_real_escape_string($this->db, $string);
2014-07-07 08:54:45 +00:00
}
2014-07-07 08:54:45 +00:00
function fetch_assoc($result)
{
2014-07-07 08:54:45 +00:00
// create an array called $row
$row = mysqli_fetch_assoc($result);
2014-07-07 08:54:45 +00:00
// return the array $row or false if none found
return $row;
}
2014-07-07 08:54:45 +00:00
function num_rows($result)
{
2014-07-07 08:54:45 +00:00
// determine row count
$num_rows = mysqli_num_rows($result);
2014-07-07 08:54:45 +00:00
// return the row count or false if none foune
return $num_rows;
}
2014-07-07 08:54:45 +00:00
function insert_id()
{
2014-07-07 08:54:45 +00:00
// Get the ID generated from the previous INSERT operation
$last_id = mysqli_insert_id($this->db);
2014-07-07 08:54:45 +00:00
// return last ID
return $last_id;
}
2014-07-07 08:54:45 +00:00
function num_fields($result)
{
$result = mysqli_num_fields($result);
2014-07-07 08:54:45 +00:00
return $result;
}
}
2014-07-07 08:54:45 +00:00
?>