If some records meet the condition I want to show them on the screen. If no record meets the condition that shows a message. I have this code and I show the data using Smarty template. But it sends me a message that the $ matrix is not defined. The table has records. The code is this:
$BD = new ConexionDB();
$sql = "SELECT COUNT(*) FROM tabla WHERE campo = 'X'";
if ($sth = $BD->query($sql)) {
if ($sth->fetchColumn() > 0) {
$sql = "SELECT cod, nom FROM tabla WHERE campo = 'X'";
while ($fila = $sth->fetch(PDO::FETCH_ASSOC)) {
$matriz[] = new DatosVO($fila['cod'], $fila['nom']);
}
$NoRegistros = "";
} else {
$NoRegistros = "NO EXISTEN REGISTROS";
$matriz = '';
}
$exito = TRUE;
}
$tpl = new Plantilla();
if ($exito) {
$tpl->assign('NoRegistros', $NoRegistros);
$tpl->assign('lista', $matriz);
$tpl->display('mostrar.tpl.php');
} else {
$tpl->assign('mensaje', $mensaje);
$tpl->display('fallo.tpl.php');
}
I used the rowcount () in this way:
$sql = "SELECT cod, nom FROM tabla WHERE condicion = 'X'";
$sth = $BD->prepare($sql);
$sth->execute();
if ($sth->rowCount() > 0) {
while ($fila = $sth->fetch(PDO::FETCH_ASSOC)) {
$matriz[] = new DatosVO($fila['cod'], $fila['nom']);
}
$NoRegistros = "";
} else {
$NoRegistros = "NO EXISTEN REGISTROS";
$prenonomstuds = "";
}
But in the PHP manual link example # 2 says that rowcount does not return the records of a select and recommend using query ().