Skip to content

Instantly share code, notes, and snippets.

@HugoSilvaF
Created February 24, 2016 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoSilvaF/62500d818b80c83ecb46 to your computer and use it in GitHub Desktop.
Save HugoSilvaF/62500d818b80c83ecb46 to your computer and use it in GitHub Desktop.
/*
Copyright © 2016 CrazyDev
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
<?php
class Database {
private $host = "localhost";
private $username = "root";
private $password = "root";
private $database = "wolfsdev";
private $databasedao;
private $connection;
public function Database() {
if(func_num_args() > 0) {
call_user_func_array(array($this, "setFields"), func_get_args());
}
$this->getMyConnection();
$this->getDatabaseDAO();
if ($this->connection->connect_errno) {
echo 'Falied to connect mysqli, error: '.$this->connection->connect_error;
}
}
public function getDatabaseDAO() {
if(!isset($this->databasedao)) {
$this->databasedao = new DatabaseDAO($this->connection);
}
return $this->databasedao;
}
public function getMyConnection() {
if(!isset($this->connection)) {
$this->connection = new mysqli($this->host, $this->username, $this->password, $this->database) or die('Could not connect to server.' );
}
return $this->connection;
}
public function cleanUpDB() {
if(!isset($this->connection)) {
mysqli_close($this->connection);
}
}
public function cleanUpDBDAO() {
if(isset($this->databasedao)) {
unset($this->databasedao);
}
}
private function setFields($host, $username, $password, $database) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->database = $database;
parent::Database();
}
}
class DatabaseDAO {
private $connection;
public function DatabaseDAO($obj) {
$this->connection = $obj;
}
public function execute($sql) {
$query = $this->connection->query($sql);
return $query;
}
}
?>
/*
Copyright © 2016 CrazyDev
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
<?php
require_once 'database.php';
class SystemControl extends Database {
/*
Construtor
*/
public function SystemControl() {
parent::Database();
}
/*
Registra um novo usuário
EX: registerUser('AAAAAA', 'Hugo L. Silva F.', 'AAD@gmail.com', 'A');
*/
function registerUser($username, $customname, $email, $password) {
$this->insertValue('accounts', 'username, customname, email, password', "'$username', '$customname', '$email', '$password'");
}
/*
Deleta usuário pelo $username
EX: getCustomname("Crazy");
*/
function deleteUser($username) {
$this->deleteValue('accounts', "`username`='$username'");
}
/*
Pega o email pelo $username
EX: getCustomname("Crazy");
*/
function getEmail($username) {
return $this->getValue('accounts', 'email', 'username='.$username);
}
/*
Pega o password do usuário pelo $username
EX: getCustomname("Crazy");
*/
function getPassword($username) {
return $this->getValue('accounts', 'password', 'username='.$username);
}
/*
Pega o nome customizado do usuário pelo $username
EX: getCustomname("Crazy");
*/
function getCustomname($username) {
return $this->getValue('accounts', 'customname', "`username`='$username'");
}
/*
Pega um valor de uma de uma coluna
*/
function getValue($table, $column, $where) {
$sql = 'SELECT * FROM `'.$table.'` WHERE '.$where;
$query = $this->getDatabaseDAO()->execute($sql) or die($this->getMyConnection()->error);
$fetch = mysqli_fetch_array($query) or die($this->getMyConnection()->error);
$value = $fetch[$column];
return $value;
}
/*
Insere um valor na tabela
EX: insertValue('accounts', 'username, customname, email, password', "'CRAZY', 'Hugo', 'hugo@gmail.com', 'minhasenha'");
*/
function insertValue($table, $columns, $values) {
$sql = 'INSERT INTO '.$table.'('.$columns.') VALUES ('.$values.');';
$query = $this->getDatabaseDAO()->execute($sql);
return $query;
}
/*
Realiza um update na tabela
EX: updateValue("accounts", "`password`='100000'", "`username`='Crazy'");
*/
function updateValue($table, $values, $where) {
$sql = 'UPDATE `'.$table.'` SET '.$values.' WHERE '.$where;
$query = $this->getDatabaseDAO()->execute($sql) or die($this->getMyConnection()->error);
return $query;
}
/*
Deleta um valor da tabela
EX: deleteUser("AAAAAA");
*/
function deleteValue($table, $where) {
$sql = 'DELETE FROM `'.$table.'` WHERE '.$where;
$query = $this->getDatabaseDAO()->execute($sql) or die($this->getMyConnection()->error);
return $query;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment