Jump to content

Recommended Posts

This might be slightly off topic now, but the problem with your original query was due to a rogue DESC. The original:

 

$sql = mysql_query("SELECT * FROM products, categories WHERE products.category = '$categoryurl' DESC LIMIT 6");

The fixed:

 

$sql = mysql_query("SELECT * FROM products, categories WHERE products.category = '$categoryurl' LIMIT 6");

You are right the ORDER BY clause was missing.

 

Now from what Nightsly was suggesting on having to queries instead of one gigantic query where I could take care of categories and products category field changed now to name field and use either or. Now after dividing it into to queries I obtained this, but it is only displaying the price and product_name field, but beyond that it seems the code is getting confused.

<?php

$dynamicList = "";
$sql= mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 6");
$productCount = mysql_num_rows($sql); 
if ($productCount>0 )
     {
     while($row = mysql_fetch_array($sql))
	          {
				$category_id= $row["category_id"];  
	            $id= $row["id"];
				$product_name= $row["product_name"];
				$price = $row["price"];
				$category = $row["category"];
				$subcategory = $row["subcategory"];
				$location = $row["location"];
				$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
                   $dynamicList .= '

<div class="dynamicList">

<div class="alone">
<div class="dynamicListF"><a href="product.php?id=' . $id . '"><strong>' . $product_name . '</strong></a></div>
<a href="product.php?id=' . $id . '">
<img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '                   "align="left" width="178" height="230" border="1" /></a><br />
     
<div class="Divinfo">
     <div >
       <p >Price:</p>
       <p >  $' . $price . '</p> 
     </div>';
 ?>
  <?php 
  $sql3= mysql_query("SELECT name FROM categories WHERE id = ". $category_id . ""); 
$productCount = mysql_num_rows($sql3); 
if ($productCount>0 )
     {
'<div class="clear"></div>
     <div >
       <p >Category:</p>
       <p> ' . $name . '</p> 
     </div>';
 }
 ?>
     <?php 

 '<div>
 <div class="clear"></div>
        <p >Sub:</p>
        <p> ' . $subcategory. '</p> 
     </div>

   <div class="clear"></div>
  <div class="city">
        <p> ' . $location. '</p>  
      </div>

     </div>
  </div>  


</div>


</div>
';
}} else {
$dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>

 

The INNER JOIN  ON made it so fa r the query below join all the fields of both when category_id in the products table is equal to the id in the categories table

 

<?php

SELECT * FROM products INNER JOIN categories ON category_id = categories.id

 

?>

 

 

co.ador's Avatar

 

Join Date: Apr 2009

Posts: 994

 

 

I thought I had it but not, Now it is displaying a error when I specify the fields in the SELECT clause

 

SELECT products.id, categories.id, category_id, name, product_name, price, subcategory, location, date_added FROM products INNER JOIN categories ON category_id = categories.id"

 

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"' at line 1

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.