franzwarning Posted July 22, 2012 Share Posted July 22, 2012 Hi there, Say I have some table, foo, and I want to select all rows in foo where 'name' is a member of an array of names that I have. How would I go about doing that? Cheers, George Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/ Share on other sites More sharing options...
requinix Posted July 22, 2012 Share Posted July 22, 2012 Depends on the size of the array and where it's coming from. But in general you can WHERE field IN ("value 1", "value 2", "value 3") or WHERE (field = "value1" OR field = "value 2" OR field = "value 3") Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/#findComment-1363405 Share on other sites More sharing options...
fenway Posted July 22, 2012 Share Posted July 22, 2012 I'd definitely steer you away from multiple ORs -- just too error-prone -- and it's easy to add one more for NULLs. Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/#findComment-1363494 Share on other sites More sharing options...
Riparian Posted July 23, 2012 Share Posted July 23, 2012 One way would be to select * and in the while loop use if(in_array($row[thesurname],$the_array_of_names)){do something;} Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/#findComment-1363628 Share on other sites More sharing options...
requinix Posted July 23, 2012 Share Posted July 23, 2012 One way would be to select * and in the while loop use if(in_array($row[thesurname],$the_array_of_names)){do something;} No. Very no. Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/#findComment-1363639 Share on other sites More sharing options...
premiso Posted July 23, 2012 Share Posted July 23, 2012 One way would be to select * and in the while loop use if(in_array($row[thesurname],$the_array_of_names)){do something;} I use this method all the time. It is very efficient and does not take long at all, +1 imo. No. Very no. Stop being a negative nancy. Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/#findComment-1363695 Share on other sites More sharing options...
Jessica Posted July 23, 2012 Share Posted July 23, 2012 Partly it depends on where the list of names is coming from. If it's another table, use a JOIN. Otherwise, use IN. Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/#findComment-1363697 Share on other sites More sharing options...
premiso Posted July 23, 2012 Share Posted July 23, 2012 Depends on the size of the array and where it's coming from. But in general you can WHERE field IN ("value 1", "value 2", "value 3") or WHERE (field = "value1" OR field = "value 2" OR field = "value 3") mm Double quotes work for string values in MySQL? Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/#findComment-1363703 Share on other sites More sharing options...
Jessica Posted July 23, 2012 Share Posted July 23, 2012 Yes, they do. Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/#findComment-1363707 Share on other sites More sharing options...
premiso Posted July 23, 2012 Share Posted July 23, 2012 Yes, they do. apparently so....apparently so. Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/#findComment-1363710 Share on other sites More sharing options...
fenway Posted July 25, 2012 Share Posted July 25, 2012 One way would be to select * and in the while loop use if(in_array($row[thesurname],$the_array_of_names)){do something;} That can't possibly be efficient for at least 23 different reasons. Link to comment https://forums.phpfreaks.com/topic/266065-see-if-item-in-table-has-attribute-in-array/#findComment-1364138 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.