MySQLi Easy Class

<?php #========================== #Dreksoft MySQLi Class V1 #Last Update: 14/01/15 #========================== class mysql_class { var $mysql; var $dbhost; var $dbuser; var $dbpass; var $dbbase; function mysql_class() { $this->mysql = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbbase); if($this->mysql->connect_errno) { die("Error al conectar"); } } function query($query) { $return = $this->mysql->query($query); if($this->mysql->error) { die("Query Error: ".$query." | ". $this->mysql->error); } return $return; } function count_rows($tabla, $argumentos = NULL, $extra = NULL) { $string = ""; if(isset($argumentos)) { if(!is_array($argumentos)) { die("Count Rows: Argumentos no es un Array"); } $string = "WHERE "; foreach($argumentos as $key => $value) { $args[] = "`".$key."` = '".$value."'"; } $string = implode(" AND ", $args); } $return_array = $this->query("SELECT COUNT(*) as total FROM ".$tabla." ".$string." ".$extra); $return_array = $return_array->fetch_array(); return $return_array["total"]; } function insert($tabla, $argumentos) { if(!is_array($argumentos)) { die("Insert: Argumentos no es un Array"); } foreach($argumentos as $columna => $value) { $args1[] = "`".$columna."`"; $args2[] = "'".$value."'"; } $args1 = implode(", ", $args1); $args2 = implode(", ", $args2); return $this->query("INSERT INTO ".$tabla." (".$args1.") VALUES (".$args2.")"); } function update($tabla, $argumentos1, $argumentos2 = NULL, $extra = NULL) { $args1 = ""; $args2 = ""; if(!is_array($argumentos1)) { die("Update: Argumentos1 no es un Array"); } foreach($argumentos1 as $key => $value) { $args1[] = "`".$key."` = '".$value."'"; } $args1 = implode(", ", $args1); if(isset($argumentos2)) { if(!is_array($argumentos2)) { die("Update: Argumentos2 no es un Array"); } foreach($argumentos2 as $key => $value) { $args2[] = "`".$key."` = '".$value."'"; } $args2 = implode(" AND ", $args2); $args1 .= " WHERE "; } return $this->query("UPDATE ".$tabla." SET ".$args1." ".$args2." ".$extra); } } ?>
To use:
Create a Class extends of mysql_class with the vars

Example:
class mysql extends mysql_class {
function mysql() {
$this->dbhost = "";
$this->dbuser = "";
$this->dbpass = "";
$this->dbbase = "";
$this->mysql_class();
}

}

i can accept comments on spanish or english

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.