Jax2 Posted March 19, 2010 Share Posted March 19, 2010 Hi all, I have 2 tables set up, one is for recipes the other is for categories. The pertinent information is stored as such: Table Recipes: recipeID - Unique - Auto increment recipe_name Table Categories: ID - unique, auto inc. category_name Now, I am trying to do an sql query to show the recipes. I need to show: Recipe ID - Recipe Name - Category Name, in that order. Here is what I have come up with (and is not working): $sql = "SELECT ".$prefix."recipes.*, ".$prefix."categories.ID, ".$prefix."categories.category_name FROM ".$prefix."recipes JOIN ".$prefix."recipes ON ".$prefix."recipes.category = ".$prefix."categories.ID order by ".$prefix."recipes.recipeID asc"; $result = mysql_query($sql, $db); if (!$result) { echo "There were no records"; die(); }; It is not returning any results. It should be showing me: Recipe ID, Recipe Name, Category Name 1, French Toast, Breakfast 2, French Toast 2, Breakfast Can't figure out what I did wrong! Sorry for all the $prefix 's ... it's a pain, but needed Quote Link to comment https://forums.phpfreaks.com/topic/195796-having-issues-with-join-anyone-care-to-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 19, 2010 Share Posted March 19, 2010 if (!$result) { That conditional test only checks of the query executed or not. It does not test if there were any matching rows. Start by finding out why the query failed (for all we know, the $db connection does not exist) - if(!$result){ // the query failed, for debugging purposes, echo the query and mysql_error() to find out why echo "The query failed: $sql<br />Because: " . mysql_error(); die(); } Quote Link to comment https://forums.phpfreaks.com/topic/195796-having-issues-with-join-anyone-care-to-help/#findComment-1028549 Share on other sites More sharing options...
Jax2 Posted March 19, 2010 Author Share Posted March 19, 2010 Okay, makes sense. I added that and this is what I am seeing: The query failed: SELECT ts_recipes.*, ts_categories.ID, ts_categories.category_name FROM ts_recipes JOIN ts_recipes ON ts_recipes.category = ts_categories.ID order by ts_recipes.recipeID asc Because: Not unique table/alias: 'ts_recipes' Which I guess i don't understand. I'm looking right at the "ts_recipes" table in phpMyAdmin Quote Link to comment https://forums.phpfreaks.com/topic/195796-having-issues-with-join-anyone-care-to-help/#findComment-1028561 Share on other sites More sharing options...
PFMaBiSmAd Posted March 19, 2010 Share Posted March 19, 2010 FROM ts_recipes JOIN ts_recipes You are joining the table to itself, rather than to the second table name. Quote Link to comment https://forums.phpfreaks.com/topic/195796-having-issues-with-join-anyone-care-to-help/#findComment-1028564 Share on other sites More sharing options...
Jax2 Posted March 19, 2010 Author Share Posted March 19, 2010 Duh. :-\ JOIN ts_categories fixed that, now it works perfectly. Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/195796-having-issues-with-join-anyone-care-to-help/#findComment-1028566 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.