<?php
class ProcuradorService {
var $username = "root";
var $password = "";
var $server = "localhost";
var $port = "";
var $databasename = "procuradores";
var $tablename = "procurador";
var $connection;
public function __construct() {
$this->connection = mysqli_connect(
$this->server,
$this->username,
$this->password,
$this->databasename,
$this->port
);
$this->throwExceptionOnError($this->connection);
}
public function getAllProcurador() {
$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename");
$this->throwExceptionOnError();
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();
$rows = array();
mysqli_stmt_bind_result($stmt, $row->nombre, $row->apellidos, $row->provincia, $row->comision, $row->cargo, $row->grupo, $row->observaciones, $row->foto, $row->id_procurador, $row->email);
while (mysqli_stmt_fetch($stmt)) {
$rows[] = $row;
$row = new stdClass();
mysqli_stmt_bind_result($stmt, $row->nombre, $row->apellidos, $row->provincia, $row->comision, $row->cargo, $row->grupo, $row->observaciones, $row->foto, $row->id_procurador, $row->email);
}
mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
return $rows;
}
public function getProcuradorByID($itemID) {
$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where id_procurador=?");
$this->throwExceptionOnError();
mysqli_stmt_bind_param($stmt, 'i', $itemID);
$this->throwExceptionOnError();
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();
mysqli_stmt_bind_result($stmt, $row->nombre, $row->apellidos, $row->provincia, $row->comision, $row->cargo, $row->grupo, $row->observaciones, $row->foto, $row->id_procurador, $row->email);
if(mysqli_stmt_fetch($stmt)) {
return $row;
} else {
return null;
}
}
public function createProcurador($item) {
$stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (nombre, apellidos, provincia, comision, cargo, grupo, observaciones, foto, email) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
$this->throwExceptionOnError();
mysqli_stmt_bind_param($stmt, 'sssssssss', $item->nombre, $item->apellidos, $item->provincia, $item->comision, $item->cargo, $item->grupo, $item->observaciones, $item->foto, $item->email);
$this->throwExceptionOnError();
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();
$autoid = mysqli_stmt_insert_id($stmt);
mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
return $autoid;
}
public function updateProcurador($item) {
$stmt = mysqli_prepare($this->connection, "UPDATE $this->tablename SET nombre=?, apellidos=?, provincia=?, comision=?, cargo=?, grupo=?, observaciones=?, foto=?, email=? WHERE id_procurador=?");
$this->throwExceptionOnError();
mysqli_stmt_bind_param($stmt, 'sssssssssi', $item->nombre, $item->apellidos, $item->provincia, $item->comision, $item->cargo, $item->grupo, $item->observaciones, $item->foto, $item->email, $item->id_procurador);
$this->throwExceptionOnError();
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();
mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
}
public function deleteProcurador($itemID) {
$stmt = mysqli_prepare($this->connection, "DELETE FROM $this->tablename WHERE id_procurador = ?");
$this->throwExceptionOnError();
mysqli_stmt_bind_param($stmt, 'i', $itemID);
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();
mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
}
public function count() {
$stmt = mysqli_prepare($this->connection, "SELECT COUNT(*) AS COUNT FROM $this->tablename");
$this->throwExceptionOnError();
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();
mysqli_stmt_bind_result($stmt, $rec_count);
$this->throwExceptionOnError();
mysqli_stmt_fetch($stmt);
$this->throwExceptionOnError();
mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
return $rec_count;
}
public function getProcurador_paged($startIndex, $numItems) {
$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename LIMIT ?, ?");
$this->throwExceptionOnError();
mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems);
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();
$rows = array();
mysqli_stmt_bind_result($stmt, $row->nombre, $row->apellidos, $row->provincia, $row->comision, $row->cargo, $row->grupo, $row->observaciones, $row->foto, $row->id_procurador, $row->email);
while (mysqli_stmt_fetch($stmt)) {
$rows[] = $row;
$row = new stdClass();
mysqli_stmt_bind_result($stmt, $row->nombre, $row->apellidos, $row->provincia, $row->comision, $row->cargo, $row->grupo, $row->observaciones, $row->foto, $row->id_procurador, $row->email);
}
mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
return $rows;
}
private function throwExceptionOnError($link = null) {
if($link == null) {
$link = $this->connection;
}
if(mysqli_error($link)) {
$msg = mysqli_errno($link) . ": " . mysqli_error($link);
throw new Exception('MySQL Error - '. $msg);
}
}
}
?>
Por si ayuda aquí esta el codigo de ProcuradorService.php