jbrill Posted March 18, 2008 Share Posted March 18, 2008 hey just looking for some quick help with this join statement thnx $sql = mysql_query("SELECT make.id, make.make FROM dynamic_vehicles, dynamic_makes WHERE vehicles.id = make.id"); while($make = mysql_fetch_array($sql)) { echo '<li><a href="#" title="'.$make['make'].'">'.$make['make'].'</a></li> '; } Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/ Share on other sites More sharing options...
Caberman Posted March 18, 2008 Share Posted March 18, 2008 You need to alias the tables. Try this: $sql = mysql_query("SELECT make.id, make.make FROM dynamic_vehicles AS vehicles, dynamic_makes AS make WHERE vehicles.id = make.id"); Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-495250 Share on other sites More sharing options...
jbrill Posted March 18, 2008 Author Share Posted March 18, 2008 PERFECT! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-495251 Share on other sites More sharing options...
jbrill Posted March 18, 2008 Author Share Posted March 18, 2008 actually, one more thing... here is the code right now: $sql = mysql_query("SELECT make.id, make.make,vehicles.active FROM dynamic_vehicles AS vehicles, dynamic_makes AS make WHERE vehicles.id = make.id ORDER BY make.make ASC"); while($make = mysql_fetch_array($sql)) { if($make['active']==1) { echo '<li><a href="#" title="'.$make['make'].'">'.$make['make'].'</a></li> '; } } I have a field in the "dynamic_vehicles" table called "active" if active is "1" then i want it to show the models that are active and not the other ones. I tried adding "AND vehicles.active='1' " but it did not work Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-495257 Share on other sites More sharing options...
Caberman Posted March 18, 2008 Share Posted March 18, 2008 Could you post your new WHERE clause? I presume it looks like: WHERE vehicles.id = make.id AND vehicles.active = '1' Also, what error do you get? Also, I usually like to run the queries directly against the DB to make sure you are able to get results and aren't confusing queries that return no results with a PHP problem. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-495285 Share on other sites More sharing options...
jbrill Posted March 18, 2008 Author Share Posted March 18, 2008 here is the code i jsut ran in phpmyadmin SELECT make.id, make.make,vehicles.active FROM dynamic_vehicles AS vehicles, dynamic_makes AS make WHERE vehicles.id = make.id AND vehicles.active='1' ORDER BY make.make ASC this is showing all the models in the "models" table i need to only show the model IF a vehicle in the "vehicles" table is active=1 Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-495290 Share on other sites More sharing options...
Caberman Posted March 18, 2008 Share Posted March 18, 2008 Neither of your tables are 'models'. Do you mean Make? Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-495295 Share on other sites More sharing options...
jbrill Posted March 18, 2008 Author Share Posted March 18, 2008 sorry i meant "make" Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-495299 Share on other sites More sharing options...
treehugger88 Posted March 18, 2008 Share Posted March 18, 2008 Swap to using the other table name. SELECT make.id, make.make,vehicles.active FROM dynamic_vehicles AS vehicles, dynamic_makes AS make WHERE vehicles.id = make.id AND MAKE.active='1' ORDER BY make.make ASC Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-495328 Share on other sites More sharing options...
jbrill Posted March 18, 2008 Author Share Posted March 18, 2008 noope, now im getting an sql error: MySQL said: #1054 - Unknown column 'make.active' in 'where clause' Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-495332 Share on other sites More sharing options...
treehugger88 Posted March 18, 2008 Share Posted March 18, 2008 huh. then there isnt a field names active in the dynamic_makes table and you did want to check vehicles.active for sure. Caberman should have told you to use a join the right way. try this SELECT make.id, make.make,vehicles.active FROM dynamic_vehicles AS vehicles, dynamic_makes AS make INNER JOIN make ON vehicles.id=make.id WHERE vehicles.active='1' ORDER BY make.make ASC not sure... does that work better? Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-495340 Share on other sites More sharing options...
jbrill Posted March 19, 2008 Author Share Posted March 19, 2008 nope, that didnt work either, any other suggestions guys? Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-496182 Share on other sites More sharing options...
BlueSkyIS Posted March 19, 2008 Share Posted March 19, 2008 what "didn't work?" can we see the updated code? Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-496185 Share on other sites More sharing options...
jbrill Posted March 19, 2008 Author Share Posted March 19, 2008 Well basically I need the to check against dynamic_vehicles and only show the "model" from the dynamic_models table if the vehicle is set to active=1 here is the code so far: $sql = mysql_query("SELECT make.id, make.make,vehicles.active FROM dynamic_vehicles AS vehicles, dynamic_makes AS make WHERE vehicles.id = make.id AND vehicles.active='1' ORDER BY make.make ASC"); while($make = mysql_fetch_array($sql)) { echo '<li><a href="#" title="'.$make['make'].'">'.$make['make'].'</a></li> '; } Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-496194 Share on other sites More sharing options...
sasa Posted March 19, 2008 Share Posted March 19, 2008 can you post structure of your tables Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-496197 Share on other sites More sharing options...
BlueSkyIS Posted March 19, 2008 Share Posted March 19, 2008 i suggest that you check for SQL errors using mysql_query($sql) or die(mysql_error()) Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-496200 Share on other sites More sharing options...
jbrill Posted March 19, 2008 Author Share Posted March 19, 2008 this is the error im getting: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/dynamica/public_html/test/includes/header.php on line 27 here is the tables structure: dynamic_vehicles: id,date_added,cat,make,model,year,vin,odometer,color,interior_color,motor,transmission,drive,doors,passengers,description,warranty,options,price,stock_num,active,sold,featured dynamic_make: id, make Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-496209 Share on other sites More sharing options...
BlueSkyIS Posted March 19, 2008 Share Posted March 19, 2008 i suggest that you check for SQL errors using mysql_query($sql) or die(mysql_error()) Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-496214 Share on other sites More sharing options...
jbrill Posted March 19, 2008 Author Share Posted March 19, 2008 k i check it and got this: Unknown column 'make.id' in 'field list' Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-496222 Share on other sites More sharing options...
BlueSkyIS Posted March 19, 2008 Share Posted March 19, 2008 so now you know your SQL is wrong. Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-496233 Share on other sites More sharing options...
jbrill Posted March 19, 2008 Author Share Posted March 19, 2008 ok, so i got some models being displayed, but they are not the proper ones that are set to active=1 here is my code $sql = mysql_query("SELECT make.id, make.make,vehicles.active FROM dynamic_vehicles AS vehicles, dynamic_makes AS make WHERE vehicles.active='1' AND vehicles.id = make.id GROUP BY vehicles.make ORDER BY make.make ASC") or die(mysql_error()); while($make = mysql_fetch_array($sql)) { if($make['active']==1) { echo '<li><a href="#" title="'.$make['make'].'">'.$make['make'].'</a></li>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/96777-whats-wrong-with-this-join-statement/#findComment-496263 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.