I've got a problem with my php code. The problem is the WHERE clause which is filtering the specified but is also spitting out some error where the 3rd record should be gone. Here's the code:
<body>
<?php
$username="USERNAME";
$password="PASSWORD";
$database="DATABASE";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM searchacts";
$result=mysql_query($query);
$num=mysql_numrows($result);
$result = mysql_query("SELECT * FROM searchacts
WHERE category='Party Bands'");
mysql_close();
?>
<?php
$i=0;
while ($i < $num) {
$image=mysql_result($result,$i,"image");
$name=mysql_result($result,$i,"name");
$category=mysql_result($result,$i,"category");
$description=mysql_result($result,$i,"description");
$stamps=mysql_result($result,$i,"stamps");
$stickmen=mysql_result($result,$i,"stickmen");
$price=mysql_result($result,$i,"price");
$view=mysql_result($result,$i,"view");
$actpagelink=mysql_result($result,$i,"actpagelink");
?>
<a href="<?php echo $actpagelink; ?>" class="searchitem">
<div class="searchimage"><img src="<?php echo $image; ?>"/></div>
<div class="searchtext">
<div class="searchname"><?php echo $name; ?></div>
<div class="searchcategory"><?php echo $category; ?></div>
<div class="searchdescription"><?php echo $description; ?></div>
</div>
<div class="searchstamps"><img src="<?php echo $stamps; ?>" /></div>
<div class="searchstickmen"><img src="<?php echo $stickmen; ?>" /></div>
<div class="searchprice"><span class="pricefrom">from</span><?php echo $price; ?></div>
<div class="searchview"><img src="<?php echo $view; ?>" /></div>
</a>
<?php
$i++;
}
?>
</body>
Its probably something very simple, This is the error:
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 2 on MySQL result index 3 in /home/enterta1/public_html/searchtestingv1.php on line 31
Which goes on for a number of lines
Your $num
counts all the fetched sets/rows in the following statement:
SELECT * FROM searchacts
while, you're trying to output data only for a selected few of them:
SELECT * FROM searchacts WHERE category='Party Bands'
Since, the second query always results in rows less than or equal to the first one; you get that error.
You have a typo in
$num=mysql_numrows($result);
Should be
$num = mysql_num_rows($result);
You are finding the number of rows
$num=mysql_numrows($result);
Before your last query, so $num doesn't represent the number of rows returned by your last query.
$result = mysql_query("SELECT * FROM searchacts WHERE category='Party Bands'");
WHERE
seems ok, but... to me, this is a strange way to pull data from the database.
Do this:
put mysql_close()
at the end of your file
no need to get $num
after the line $result=...
do:
while ($row=mysql_fetch_assoc($result)) $acts[]=$row;
// now all your records are saved in array $acts, e.g.
// $acts[0]['name'], $acts[0]['category'], where...
// the first dimension of the array [0] contains the number of the record...
// the second the field-name from your database!
to output all of $acts do:
foreach ($acts as $act) { ?>
...
<div class="searchimage"><img src="<?=$act['image']?>"/></div>
<div class="searchtext">
<div class="searchname"><?=$act['name']?></div>
<div class="searchcategory"><?=$act['category']?></div>
<div class="searchdescription"><?=$act['description']?></div>
</div>
... <? // you get the idea ;-)
} // foreach