Reputation: 11
I am trying to show all results from a database which have the same category.
I have managed to print one row from the database just wondering how i do this for the rest?
here is my code
<?php
$catagory=$_GET["q"];
$con = mysql_connect("localhost","cl49-XXX","XXX");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
@mysql_select_db("cl49-vogalcms", $con)or die( "Unable to select database");
$result=mysql_query("SELECT * FROM products WHERE catagory = '$catagory' ")or die('You need enter a catagoryE ' );
$row = mysql_fetch_array($result);
$prodname=$row['prodname'];
$prodID=$row['prodID'];
echo"Catagory: $catagory <br /> ID $prodID<br /> name $prodname";
if ($swt==0) {$swt=1;} else {$swt=0;}
?>
Upvotes: 0
Views: 64
Reputation: 38645
You can run a loop like following:
$rowCount = mysql_num_rows($result);
if ($rowCount > 0) {
while($row = mysql_fetch_array($result)) {
$prodname=$row['prodname'];
$prodID=$row['prodID'];
echo"Catagory: $catagory <br /> ID $prodID<br /> name $prodname";
}
} else {
echo "No Products are available";
}
A couple of other points to note are:
@mysql_select_db
. Since you already have or die(...
, remove the @
in front of mysql_select_db
. This is because you want to see the errors if they occurred.mysqli
or PDO
as mysql
is deprecated.Upvotes: 4
Reputation: 456
You have to loop through the result set.
while($row = mysql_fetch_assoc($result)) {
$prodname=$row['prodname'];
$prodID=$row['prodID'];
echo"Catagory: $catagory <br /> ID $prodID<br /> name $prodname";
}
Upvotes: 0