perky416 Posted May 19, 2011 Share Posted May 19, 2011 Hi everyone, On my website people can add their domain names in a text box. I then use a foreach to validate each domain the text box. The code is similar to: foreach($lines as $line){ $count_query = mysql_query("SELECT * FROM domains WHERE domain='$line'"); $count = mysql_num_rows($count_query); if ($count > 0){ $error[] = $line . " already exists!<br />"; } } I was wondering if anybody knows a way to use the mysql_query outside of the foreach loop, as if the person has entered 100 domains for example, thats 100 queries accessing my database. Im not too sure if its possible or not. Anybody have any ideas? Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/236887-removing-mysql_query-from-foreach/ Share on other sites More sharing options...
wildteen88 Posted May 19, 2011 Share Posted May 19, 2011 Yes, you need to dynamically generate your query, eg. $domains_list = implode("','", array_map('trim', $lines)); $query = "SELECT domain FROM domains WHERE domain IN('$domains_list')"; $result = mysql_query($query); That will query the database for all entered domains. You should validate the domains the user has entered. Drop any invalid domains before querying the database. Quote Link to comment https://forums.phpfreaks.com/topic/236887-removing-mysql_query-from-foreach/#findComment-1217676 Share on other sites More sharing options...
perky416 Posted May 19, 2011 Author Share Posted May 19, 2011 Thanks wild teen you've come to my rescue once again . A problem im having though is that currently i use the foreach to display each domain that doesn't exist. test.com already exists. test1.com already exists. Using your code, iv tried to add the "already exists" error to each domain that already exists. I have came up with the following which does a good job of displaying the error for each domain, however obviously it also displays the error for domains that dont already exist. Do you know how id be able to modify it for only domains that dont exists? $domains_list = implode("','", array_map('trim', $lines)); $query = "SELECT domain FROM domains WHERE domain IN('$domains_list')"; $result = mysql_query($query); $count = mysql_num_rows($result); if ($count > 0){ $exists = explode("','",$domains_list); foreach ($exists as $exist){ $error[] = $exist . " is already in use"; } } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/236887-removing-mysql_query-from-foreach/#findComment-1217762 Share on other sites More sharing options...
DavidAM Posted May 19, 2011 Share Posted May 19, 2011 The ones that already exist, will come back from the query. The ones that don't, won't. $domains_list = implode("','", array_map('trim', $lines)); $query = "SELECT domain FROM domains WHERE domain IN('$domains_list')"; $result = mysql_query($query); $exists = array(); $count = mysql_num_rows($result); if ($count > 0){ while ($row = mysql_fetch_assoc($result)) { $exists[] = $row['domain']; $error[] = $row['domain'] . " is already in use"; } } $not_exists = array_diff(array_map('trim', $lines), $exists); Quote Link to comment https://forums.phpfreaks.com/topic/236887-removing-mysql_query-from-foreach/#findComment-1217768 Share on other sites More sharing options...
perky416 Posted May 20, 2011 Author Share Posted May 20, 2011 Sorted Thanks i appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/236887-removing-mysql_query-from-foreach/#findComment-1218212 Share on other sites More sharing options...
Psycho Posted May 20, 2011 Share Posted May 20, 2011 The ones that already exist, will come back from the query. The ones that don't, won't. $domains_list = implode("','", array_map('trim', $lines)); $query = "SELECT domain FROM domains WHERE domain IN('$domains_list')"; $result = mysql_query($query); $exists = array(); $count = mysql_num_rows($result); if ($count > 0){ while ($row = mysql_fetch_assoc($result)) { $exists[] = $row['domain']; $error[] = $row['domain'] . " is already in use"; } } $not_exists = array_diff(array_map('trim', $lines), $exists); FYI the following lines serve no purpose in the above code $count = mysql_num_rows($result); if ($count > 0){ The while() loop will take care of an empty result set as it is. Quote Link to comment https://forums.phpfreaks.com/topic/236887-removing-mysql_query-from-foreach/#findComment-1218228 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.