Error when accessing data

Hi,

I have the following code to access data :

$get=mysql_query(“SELECT * FROM products WHERE catid = '”.$cat."’");

$total=mysql_num_rows($get);
$i=1;
while($rsProducts = mysql_fetch_assoc($get))
{
?>
photo=‘pics/<?=$rsProducts['id'];?>.jpg’;
photo=photo.replace(//2//,"/");
if(document.images){
photos[<?=$i;?>]=photo;
photo3.setAttribute(“src”,photos[<?=$i;?>]);
}

<? $i++; } mysql_data_seek($get, 0); ?>

Problem is that this returns a mysql_fetch error if the query returns 0 records - which then looks ugly on the page.

Whats the best way round this?

Thanks

I am not sure if there is a better way, but I have always just done…

[php]<?php

$get=mysql_query(“SELECT * FROM products WHERE catid = '”.$cat."’");

$total=mysql_num_rows($get);
$i=1;

if($total <= 0)
{
echo “No Records Found”;
}
else
{
while($rsProducts = mysql_fetch_assoc($get))
{
?>
photo=‘pics/<?=$rsProducts['id'];?>.jpg’;
photo=photo.replace(//2//,"/");
if(document.images)
{
photos[<?=$i;?>]=photo;
photo3.setAttribute(“src”,photos[<?=$i;?>]);
}

<? $i++; } mysql_data_seek($get, 0); } ?>[/php]

PS: I wouldn’t ever use short hand opening php tags. (<?) Should always use <?php as the short hand isn't on by default in php. Also, avoid using the equals as echo. <?php= "";?> I am unsure, but I do not think that one is on by default either.

Sponsor our Newsletter | Privacy Policy | Terms of Service