I need to record into a variable if a query have or not have registers

Im having this problem that I solve it in an ugly way. I mean, there is supposed to be a function that does that for me. What i NEED is that if the query CAN`T find any records, shows a message that says, “there are no records available”. This how I solve it for now, but I supposed there must be a better way.

	while($dato2=  mysql_fetch_array($query))
			{
				$chose = $dato2["cod_tramite"]; 
				if ($chose != 0){ break;}
			}
			
		if ($chose == 0 )
		{
			echo "No se disponen registros de facturas ya pagas";
		}
		else
		{
			echo "</br>Facturas ya pagas :";
			echo "<table border = '1' ><tr><th>Fecha de emision</th><th>Fecha de vencimiento</th><th>Codigo de la factura</th><th>Coste</th><th>Saldo</th></tr>" ; 
			while($dato=  mysql_fetch_array($query))
			{
				echo "<tr>";
				echo "<td>".$dato["fecha_emi"]."</td>" ;
				echo "<td>".$dato["fecha_venc"]."</td>"  ;
			//	echo "<td>".$dato["pago"]."</td>"  ;
				echo "<td>".$dato["cod_tramite"]."</td>" ;	
				echo "<td>".$dato["coste"]."</td>" ;
				echo "<td> SALDADA </td>" ;
			echo "</tr>";			
			}
			
		echo "</table>";

Well, normally, you would just check the results of your query. Check the number of rows it returns.
If 0, id did not retrieve any results.

So, in general terms…

$query = “SELECT * FROM sometable”;
$result = mysql_query($query);
if (mysql_num_rows($result)==0) {
echo “No records found!”;
} else {
// handle your records with the rest of your code starting with the WHILE
}

As you see, you query your database. Check the count of how many records were found.
If none, say so… If some were found, display them in your table…

Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service