datoshway Posted August 14, 2010 Share Posted August 14, 2010 I need help! Banging my head against the wall on this all day. I really need some assistance on finishing this statement heres the summery. I'm using mysql version 5.2.6 I have a table set up with 3 fields in it intID intItemID intIconID This database is correlating to the intID of another database. So my statement needs to detect what Id we are on and show only those rows with that unique ID. Here is what I have so far. queryGetIcons = db_query($strQuery); if (db_num_rows($queryGetIcons) > 0) { $objRow = db_fetch_object($queryGetIcons); $strHeadline = stripslashes($objRow->strIconName); db_free_result($queryGetIcons); $strPostURL = "Icon/" . $intIconID; $strQuery = sprintf( "SELECT i.intID, i.strName FROM tblItems i LEFT JOIN tblItemToIcon c ON c.intItemID = i.intID WHERE c.intIconID = %d ORDER BY i.strName ASC", intval($intIconID)); } else { header("Location: " . STR_RELATIVE_PATH); } Quote Link to comment https://forums.phpfreaks.com/topic/210694-completing-a-join-statement/ Share on other sites More sharing options...
datoshway Posted August 14, 2010 Author Share Posted August 14, 2010 Sorry I had pasted in a older version of my code. This is the most recent that I can't seem to get working. $queryGetIcons = db_query($strQuery); if (db_num_rows($queryGetIcons) > 0) { $strFilename = stripslashes($objRow->strFilename); $txtIcon = stripslashes($objRow->txtIcon); db_free_result($queryGetIcons); $strQuery = sprintf( "SELECT i.intID, i.strName FROM tblItems i LEFT JOIN tblItemToIcon c ON c.intItemID = i.intID WHERE c.intIconID = %d ORDER BY i.strName ASC", intval($intIconID)); } else { header("Location: " . STR_RELATIVE_PATH); } Quote Link to comment https://forums.phpfreaks.com/topic/210694-completing-a-join-statement/#findComment-1099112 Share on other sites More sharing options...
kickstart Posted August 14, 2010 Share Posted August 14, 2010 Hi Which table is what? I am unsure what you are trying to retrieve. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/210694-completing-a-join-statement/#findComment-1099143 Share on other sites More sharing options...
datoshway Posted August 14, 2010 Author Share Posted August 14, 2010 Three tables are at hand here tblIcons tblItems tblItemToIcon The tbl Items is the table holding all the product data. tblIcons is the table holding all the Icon data. And tblItemToIcon is the table holding the associations to each product. That table looks like this intID intItemID i intIconID 305 27 4 The intIconID is the ID for the Icons in tblIcons, and the intItemID is ID for the product in tblItems. What we need to happen here is when the user is on ItemID it should check those rows to see if the IconID exists for the ItemID Quote Link to comment https://forums.phpfreaks.com/topic/210694-completing-a-join-statement/#findComment-1099177 Share on other sites More sharing options...
kickstart Posted August 14, 2010 Share Posted August 14, 2010 Hi I think you code should do what you want it to do. However you need to bring back a field from the tblItemToIcon table. Best to return the intIconID, as if this is NULL then you know that a matching icon record hasn't been found. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/210694-completing-a-join-statement/#findComment-1099228 Share on other sites More sharing options...
datoshway Posted August 14, 2010 Author Share Posted August 14, 2010 hmm I tried that still not returning results.. I'm lost. Maybe I did it wrong. Any chance you can display a example? Quote Link to comment https://forums.phpfreaks.com/topic/210694-completing-a-join-statement/#findComment-1099236 Share on other sites More sharing options...
kickstart Posted August 14, 2010 Share Posted August 14, 2010 Hi It should be fine. Try the following line in your code $strQuery = sprintf("SELECT i.intID, i.strName, c.intIconID FROM tblItems i LEFT OUTER JOIN tblItemToIcon c ON c.intItemID = i.intID WHERE c.intIconID = %d ORDER BY i.strName ASC",intval($intIconID)); It is doing a LEFT OUTER JOIN, so wil bring back the records from the tblItems and if they exist those from tbItemToIcon. If there isn't a matching icon then intIconID will come back as null All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/210694-completing-a-join-statement/#findComment-1099280 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.