I have a problem when making the query, it does not return anything. The query is correct since I have executed it in the sqldeveloper and it does not give me an error in the connection either. This is my code:
test.php
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>MUNICIPIOS</title>
</head>
<body>
<?php
include_once('php/clases.php');
$bd = new ConexionBD();
$consulta = 'SELECT * FROM MUNICIPIOS';
$res = $bd->consulta($consulta);
foreach($res as $fila){
print_r($fila);
}
?>
</body>
</html>
clases.php
<?php
class ConexionBD{
private $conexion;
private $host = "oci:dbname=localhost/XE";
private $usuario = "BD";
private $pass = "1234";
function __construct(){
try{
$this->conexion = new PDO($this->host, $this->usuario, $this->pass);
$this->conexion->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Conexion realizada con exito!<br>";
}catch(PDOException $e){
$this->conexion = null;
$codigo = $e->getCode();
$mensaje = $e->getMessage();
$fichero = $e->getFile();
$linea = $e->getLine();
echo "Error en la conexion! <br>
Mensaje de error: $mensaje<br>
Codigo: $codigo<br>
Fichero: $fichero<br>
Linea: $linea";
}
}
function consulta($query){
try {
$stmt= $this->conexion->query($query);
echo "Consulta realizada con exito!<br>";
}catch( PDOException $e ){
$stmt = null;
$codigo = $e->getCode();
$mensaje = $e->getMessage();
$fichero = $e->getFile();
$linea = $e->getLine();
echo "Error en la consulta! <br>
Mensaje de error: $mensaje<br>
Codigo: $codigo<br>
Fichero: $fichero<br>
Linea: $linea";
}
return $stmt;
}
function ejecuta($query){
try {
$stmt= $this->conexion->exec($query);
}catch( PDOException $e ){
$stmt = null;
$codigo = $e->getCode();
$mensaje = $e->getMessage();
$fichero = $e->getFile();
$linea = $e->getLine();
echo "Error en la ejecucion! <br>
Mensaje de error: $mensaje<br>
Codigo: $codigo<br>
Fichero: $fichero<br>
Linea: $linea";
}
return $stmt;
}
function consultaPaginada($consulta, $page_num, $page_size){
try{
$first = ($page_num - 1) * $page_size + 1;
$last = $page_num * $page_size;
$paged_query = "SELECT * FROM ( SELECT ROWNUM RNUM, AUX.* FROM( $consulta) AUX WHERE ROWNUM <= :last) WHERE RNUM >= :first";
$stmt = $this->conexion->prepare($paged_query);
$stmt->bindParam( ':first', $first );
$stmt->bindParam( ':last', $last );
$stmt->execute();
return $stmt;
}catch( PDOException $e ) {
$stmt = null;
$codigo = $e->getCode();
$mensaje = $e->getMessage();
$fichero = $e->getFile();
$linea = $e->getLine();
echo "Error en la consulta! <br>
Mensaje de error: $mensaje<br>
Codigo: $codigo<br>
Fichero: $fichero<br>
Linea: $linea";
}
}
}
?>