I’m working on the datafeed script for my Affiliate program and really thought I was “doin’ it” but when I run the script it creates the CSV file with only 7 of 28 products. Prior to testing I made all the products meet the conditions of the query so they all should be included.I even removed the stock level and price conditions. Still only 7 of 28 gets added to the csv file 5 from one category and 2 from the other. There are only 5 products in the first category and all of those are returned. But only 2 of the 23 in the second category are returned.
The Goal is to get the specific data for all products from the categories in the query and dump them into a csv file.
This is my first time trying to write a more complex sql query (for a beginner anyway).
Using print_r I found the problem is my query. I thought I had this working before. The difference between when it was working and now is that I included grabbing the categoryid and catparentid. But even if I remove these additions, I still get the same results. Now I’m thinking that I might have thought it was working before on my larger database of products but perhaps it actually wasn’t returning all of the products but because there were so many I thought they were.
The full script is pretty long but I created a new script below just to see if the query is even grabbing all of the records… it’s not. 🙁
If I run a query to check for unassociated products (i.e. ones that have no category associations), all products are associated with a category.
All of the products have image & brand associations so I don’t think that’s the issue either.
Even if I remove the Where clause altogether, only the 7 products are grabbed.
I also ran the query directly within the database and got the same thing:
Showing rows 0 - 6 ( 7 total, Query took 0.0029 sec)
SELECT isc_products.productid, isc_products.prodcode, isc_products.prodname, isc_products.produrl, isc_products.prodcalculatedprice, isc_products.proddesc, isc_products.prodcurrentinv, isc_products.upc, isc_categories.categoryid, isc_categories.catparentid, isc_categories.catname, isc_product_images.imagefilestd, isc_product_images.imagefilethumb, isc_brands.brandname
FROM isc_products
INNER JOIN isc_categories ON isc_products.prodcatids = isc_categories.categoryid
INNER JOIN isc_product_images ON isc_products.productid = isc_product_images.imageprodid
INNER JOIN isc_brands ON isc_products.prodbrandid = isc_brands.brandid
WHERE isc_categories.categoryid =10
OR isc_categories.categoryid =12
Could there be any special characters in the data that would cause this issue?
Or am I using the inner joins incorrectly?
<pre>
<?php
// Make a MySQL Connection
mysql_connect("localhost", "myusername", "mypassword") or die(mysql_error());
mysql_select_db("mydatabase") or die(mysql_error());
$result = mysql_query("SELECT isc_products.productid,
isc_products.prodcode,
isc_products.prodname,
isc_products.produrl,
isc_products.prodcalculatedprice,
isc_products.proddesc,
isc_products.prodcurrentinv,
isc_products.upc,
isc_categories.categoryid,
isc_categories.catparentid,
isc_categories.catname,
isc_product_images.imagefilestd,
isc_product_images.imagefilethumb,
isc_brands.brandname
FROM isc_products
INNER JOIN isc_categories
ON isc_products.prodcatids = isc_categories.categoryid
INNER JOIN isc_product_images
ON isc_products.productid = isc_product_images.imageprodid
INNER JOIN isc_brands
ON isc_products.prodbrandid = isc_brands.brandid
WHERE isc_categories.categoryid = 17
OR isc_categories.categoryid = 15
OR isc_categories.categoryid = 16
OR isc_categories.categoryid = 12
OR isc_categories.categoryid = 13
OR isc_categories.categoryid = 14
OR isc_categories.categoryid = 11
OR isc_categories.categoryid = 10
AND isc_products.prodcurrentinv > 1
AND isc_products.prodcalculatedprice > 1.00
ORDER BY isc_categories.catname
")
or die(mysql_error());
while($row = mysql_fetch_array( $result)){
print_r ($row);
}
// end of file
echo '<br/>****** Script successfully run! ******';
?>
</pre>
Any thoughts on this would be appreciated. Dislcaimer if your first thought is “Why didn’t you…” It’s because I didn’t or don’t know about it. 🙂 One day, my friend… one day I will. hahahaha
Thank you!
Try replacing the inner join with LEFT JOIN, then look at the results. Do you see the 28 you expect? If so (or you see more), the join is incorrect in some way.