Skip to content

Instantly share code, notes, and snippets.

@jsstoni
Created March 28, 2015 03:45
Show Gist options
  • Save jsstoni/761bd3ce5e6d1df6e067 to your computer and use it in GitHub Desktop.
Save jsstoni/761bd3ce5e6d1df6e067 to your computer and use it in GitHub Desktop.
mysqli basic class
<?php
/**
* @author "Jesus Perez @jsstoni19"
* @package anco
* @subpackage newSql
* @copyright Free
* @version anco 1.1
*/
abstract class newSql {
private static $host = 'localhost';
private static $user = 'root';
private static $pass = 'xxxx';
private $db = 'anco';
private $anco;
private $here;
function __construct() {
//error_reporting(0);
session_start();
try {
if (!@$this->anco = new mysqli(self::$host, self::$user, self::$pass, $this->db)) {
throw new Exception("Error", 1);
}
} catch(Exception $e) {
die("Error al conectar");
}
}
public function ResulRows($query) {
$result = $this->anco->query($query);
if ($result->num_rows > 0) {
while ($rows = $result->fetch_assoc()) {
$exec[] = $rows;
}
$result->free_result();
return $exec;
}
}
public function InsertRows($table, $data) {
$sql = "INSERT INTO ".$table."";
$sql .= " (`".implode("`, `", array_keys($data))."`)";
$sql .= " VALUES ('".implode("', '", $data)."') ";
$exec = $this->anco->query($sql);
}
public function UpdateRows($table, $data, $id) {
foreach ($data as $field=>$value) {
$fields[] = sprintf("`%s` = '%s'", $field, $value);
}
$field_list = join(',', $fields);
$query = sprintf("UPDATE `%s` SET %s WHERE ID_plug='".$id."'", $table, $field_list);
$exec = $this->anco->query($query);
}
function __destruct() {
$this->anco->close();
unset($this->anco);
}
}
/**
** Usage
* Free Query Result
* newSql->ResulRows('SELECT title, code FROM github');
* insert data
* newSql->InsertRows('github', array('title' => 'mysqli', 'code' => 'text'));
* update data
* newSql->UpdateRows('github', array('title' => 'mysqli basic class', 'code' => 'php'));
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment