PHP. Prepare statement. Dao

Desde hace tiempo estoy implementando en PHP las sentencias SQL al estilo java, PreparedStatement, con mysqli. Así evitamos cualquier inyección de código y trabajamos contra la base de datos como les gusta a los Dba, mediante Bind.



Esto sería parte de un objeto Dao implementación Mysql, fundamental el bean para trabajar con objetos y dar de una vez el salto a la programación POO.


public function buscarPorTitulo( $_titulo ) {

$Noticia_ = new Noticia();
$Noticias_array = array();

$sql = "SELECT ID_NOTICIA, TITULO, FECHA, CSS_STYLE, RUTA_FICHERO_HTML ";
$sql .= "FROM NOTICIAS ";
$sql .= "WHERE TITULO LIKE ? ";

if ($stmt = $this->mysqli->prepare($sql)) {

$stmt->bind_param("s", $_titulo);
/*
Character Description
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
*/
$result = $stmt->execute();
if (!$result) {
throw new Exception( "Error al consultar BB.DD" );
}


$Noticias_array_ = array();

$_stmt->bind_result(
$idNoticia,
$titulo,
$fecha,
$css_style,
$rutaFicheroHtml
);

$i=0;
while ($_stmt->fetch()) {
$Noticia = new Noticia();
$Noticia->setIdNoticia($idNoticia);
$Noticia->setTitulo($titulo);
$Noticia->setFecha($fecha);
$Noticia->setCssStyle($css_style);
$Noticia->setRutaFicheroHtml($rutaFicheroHtml);
$Noticias_array_[$i] = $Noticia;
$i++;
}

return $Noticias_array_;

$stmt->close();

} else {
throw new Exception( "Error al consultar BB.DD" );
}

return $Noticias_array;

}




public class Noticia {

private $idNoticia=0;
private $titulo="";
private $fecha="0000-00-00";
private $cssStyle="";
private $rutaFicheroHtml="";

public function Noticia( ) {
$this->idNoticia=0;
$this->titulo="";
$this->fecha="0000-00-00";
$this->cssStyle="";
$this->rutaFicheroHtml="";
}

public function setIdNoticia( $_idNoticia ) {
$this->idNoticia=$_idNoticia;
return;
}

public function getIdNoticia( ) {
return $this->idNoticia;
}

public function setTitulo( $_titulo ) {
$this->titulo=$_titulo;
return;
}

public function getTitulo( ) {
return $this->titulo;
}

public function setFecha( $_fecha ) {
$this->fecha=$_fecha;
return;
}

public function getFecha( ) {
return $this->fecha;
}

public function setCssStyle( $_cssStyle ) {
$this->cssStyle=$_cssStyle;
return;
}

public function getCssStyle( ) {
return $this->cssStyle;
}

public function setRutaFicheroHtml( $_rutaFicheroHtml ) {
$this->rutaFicheroHtml=$_rutaFicheroHtml;
return;
}

public function getRutaFicheroHtml( ) {
return $this->rutaFicheroHtml;
}

}

Comentarios

  1. El contructor del bean lo hago al estilo java, pero PHP lo admite sin problemas, Aunque en PHP sería mediante:

    public function __construct() {
    /* No haria falta inicializar las propiedades o fields
    * ya que cogerian los valores de la declaración del objeto
    $this->idNoticia=0;
    $this->titulo="";
    $this->fecha="0000-00-00";
    $this->cssStyle="";
    $this->rutaFicheroHtml="";
    */
    }

    ResponderEliminar

Publicar un comentario

Entradas populares de este blog

PHP. módulo de un "long". Calcular dígito control de IBAN.

HTML div organizar contenido