user2643837
user2643837

Reputation: 11

all results from database

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

Answers (2)

vee
vee

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:

  1. Do not suppress errors with @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.
  2. Use mysqli or PDO as mysql is deprecated.

Upvotes: 4

Ryhnn
Ryhnn

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

Related Questions