marketboy Posted January 25, 2008 Share Posted January 25, 2008 I need to be able to pull only certain fields from my database. Instead of pulling all data from field "x" I need to pull or exclude data to certain items. $sqlDealers="SELECT id, CONCAT(first_name,' ',last_name) AS name FROM ".$db_prefix."dealers ORDER BY name"; $rsDealers=@mysql_query($sqlDealers) or trigger_error('Error on executing '.$sqlDealers, E_USER_ERROR); It's selecting the id field, but I need to either make it show only some of the items, or exclude others. Any input would be apreciated! Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/ Share on other sites More sharing options...
revraz Posted January 25, 2008 Share Posted January 25, 2008 Use a WHERE clause to say what determines what you pull. WHERE last_name = 'Jones' Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/#findComment-449269 Share on other sites More sharing options...
marketboy Posted January 29, 2008 Author Share Posted January 29, 2008 Ok, I've tried a variation of ways, but keep getting a fatal error: Fatal error: Error on executing SELECT id, WHERE id='33' CONCAT(first_name,' ',last_name) AS name FROM free_listings_dealers ORDER BY name in /home/content/m/a/r/marketmy1/html/index1-15.php on line 55 I changed the code to this: $sqlDealers="SELECT id, WHERE id='33' CONCAT(first_name,' ',last_name) AS name FROM ".$db_prefix."dealers ORDER BY name"; $rsDealers=@mysql_query($sqlDealers) or trigger_error('Error on executing '.$sqlDealers, E_USER_ERROR); Anything? Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/#findComment-452509 Share on other sites More sharing options...
revraz Posted January 29, 2008 Share Posted January 29, 2008 WHERE should be after FROM and before ORDER BY Check out the MySQL page for how to use SELECT statement. Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/#findComment-452750 Share on other sites More sharing options...
fenway Posted January 29, 2008 Share Posted January 29, 2008 Check out the MySQL page for how to use SELECT statement. So what you need to issue is this: SELECT id, CONCAT(first_name,' ',last_name) AS name FROM free_listings_dealers WHERE id='33' ORDER BY name Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/#findComment-452807 Share on other sites More sharing options...
marketboy Posted January 29, 2008 Author Share Posted January 29, 2008 Ok, great. Thanks! I thought I had done that at one point, but I had a misplaced quote. One last question now, would be if I wanted to include/exclude more than one variable? For example, if I wanted to include both id number 33 and 34. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/#findComment-452858 Share on other sites More sharing options...
marketboy Posted January 29, 2008 Author Share Posted January 29, 2008 I think I've got it by using OR id="id" Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/#findComment-452873 Share on other sites More sharing options...
revraz Posted January 29, 2008 Share Posted January 29, 2008 If you wanted it to be both, use AND, if you want it to be either use OR Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/#findComment-452899 Share on other sites More sharing options...
marketboy Posted January 29, 2008 Author Share Posted January 29, 2008 Gotcha. Now what if I wanted to select say 2-66, or 2, 4, 5, and 11-15? Is there an easier way then just setting it for each one? Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/#findComment-452906 Share on other sites More sharing options...
revraz Posted January 30, 2008 Share Posted January 30, 2008 You can do IN (2,4,5) You can do >2 AND < 66 Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/#findComment-453007 Share on other sites More sharing options...
fenway Posted January 30, 2008 Share Posted January 30, 2008 BETWEEN works too. Quote Link to comment https://forums.phpfreaks.com/topic/87825-only-pull-certain-data-from-sql/#findComment-453020 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.