markkanning Posted February 12, 2007 Share Posted February 12, 2007 Hey ya'll, I've got a MySQL select-query-with-loop problem. I'm putting together a database-driven, "store locator" page for a wholesale foods outlet. The page will have a list of the 50 states that's being created from one table, AND that will be linked depending on whether corresponding store data for that state actually exists in another table. I'm figuring the query might look something like... select states.stateid, states.statename, stores.stateid from states, stores where states.stateid = stores.stateid ...but I'm not sure as I'm stumped as to how the following loop will work. I'm guessing the loop may be something like... for ($i=0; $i <$num_results; $i++) { $row = mysql_fetch_array($result); if (no corresponding table data for this state) { echo $statename."<br>\n"; } else { echo "<a href=\"?stateid=".$stateid."\">$statename."</a><br>\n"; } } Am I on the right track? Quote Link to comment https://forums.phpfreaks.com/topic/38110-solved-selecting-data-from-2-tables-with-data-checking-loop/ Share on other sites More sharing options...
effigy Posted February 12, 2007 Share Posted February 12, 2007 Your where clause is only going to pull states that have stores. If you want to list states that do not have stores, you need to use the LEFT JOIN syntax, which will include every state and give you NULL store data when a state is without store. Quote Link to comment https://forums.phpfreaks.com/topic/38110-solved-selecting-data-from-2-tables-with-data-checking-loop/#findComment-182479 Share on other sites More sharing options...
markkanning Posted February 12, 2007 Author Share Posted February 12, 2007 Good tip, man! So, could I do something like... SELECT states.stateid, states.statename FROM states LEFT JOIN stores ON states.stateid = stores.stateid Quote Link to comment https://forums.phpfreaks.com/topic/38110-solved-selecting-data-from-2-tables-with-data-checking-loop/#findComment-182483 Share on other sites More sharing options...
markkanning Posted February 12, 2007 Author Share Posted February 12, 2007 Hold up! I just registered what you are saying the "where" clause is doing. In other words, I don't need a where clause? How, then, do I associate the stateid in the "states" table with any row(s) of stateid in the "stores" table? Quote Link to comment https://forums.phpfreaks.com/topic/38110-solved-selecting-data-from-2-tables-with-data-checking-loop/#findComment-182488 Share on other sites More sharing options...
effigy Posted February 12, 2007 Share Posted February 12, 2007 If you say where states.stateid = stores.stateid, you're only going to get states that have stores. If you say FROM states LEFT JOIN stores ON states.stateid = stores.stateid, you're going to get all the states, even if they don't have stores. Both of these statements join the tables; only the data they return differs. See this example. Quote Link to comment https://forums.phpfreaks.com/topic/38110-solved-selecting-data-from-2-tables-with-data-checking-loop/#findComment-182493 Share on other sites More sharing options...
markkanning Posted February 12, 2007 Author Share Posted February 12, 2007 I am laughing out loud, brother. It just so happens I got the syntax for my last query adjustment from that very Tizag page about 10 minutes ago! Thanks for the input, effigy! Mark Quote Link to comment https://forums.phpfreaks.com/topic/38110-solved-selecting-data-from-2-tables-with-data-checking-loop/#findComment-182500 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.