Código PHP:
<?php
class Conexion
{
private $cn;
private $rs;
private $servidor;
private $usuario;
private $password;
private $value;
private $bd;
private $puerto;
public function __construct($servidor = "localhost", $usuario = "root", $password = "220785", $bd = "sie", $puerto="3306")
{
$this->servidor = $servidor;
$this->usuario = $usuario;
$this->password = $password;
$this->bd = $bd;
$this->puerto = $puerto;
$this->cn = new mysqli($servidor, $usuario, $password, $bd, $puerto);
}
public function dbExecute($query)
{
$this->rs = $this->cn->query($query);
return $this->rs;
}
public function getInsertedId()
{
return $this->cn->insert_id;
}
public function afectados()
{
return $this->cn->affected_rows;
}
public function __destruct()
{
$this->cn;
}
}
?>
Código PHP:
<?php
class Autor extends Conexion
{
private $nombre;
private $apellidos;
private $id_autor;
private $where;
public function __construct()
{
parent::__construct();
parent::__destruct();
}
public function agregar($nombre, $apellidos)
{
$this->nombre = $nombre;
$this->apellidos = $apellidos;
$query = "INSERT INTO autores (id_autor,nombre,apellidos) VALUES ('','$nombre','$apellidos')";
return $this->dbExecute($query);
}
public function consultar($where)
{
$this->where = $where;
$query = "SELECT * FROM autores $where";
return $this->dbExecute($query);
}
public function modificar($nombre, $apellidos, $id_autor)
{
$this->nombre = $nombre;
$this->apellidos = $apellidos;
$this->id_autor = $id_autor;
$query = "UPDATE autores SET nombre='$nombre',apellidos='$apellidos' WHERE id_autor='$id_autor'";
return $this->dbExecute($query);
}
public function eliminar($id_autor)
{
$this->id_autor = $id_autor;
$query = "DELETE FROM autores WHERE id_autor='$id_autor'";
return $this->dbExecute($query);
}
public function total($id_autor)
{
$this->id_autor = $id_autor;
$query = "SELECT COUNT(id_autor) AS id_autor FROM libros WHERE id_autor='$id_autor'";
return $this->dbExecute($query);
}
}
?>
Código PHP:
include("Conexion.php");
include("Autor.php");
$autor=new Autor();
$autor->agregar("juan","perez");