Jump to content

what's wrong with this join query?


co.ador

Recommended Posts

There should be a way to put it all in one query but I am going to focus on using two queries now while the solutions of integrating it in one comes. I will research on that.

 

I am going to start integrating it with two queries.

Link to comment
Share on other sites

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");

Link to comment
Share on other sites

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();
?>

 

Link to comment
Share on other sites

You need to use a join so that it looks like:

 

SELECT * FROM products JOIN categories ON(id_of_the_products_tbl = id_of_the_categories_tbl WHERE products.category = '$categoryurl' DESC LIMIT 6"

Link to comment
Share on other sites

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

 

?>

 

Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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.