Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/236887-removing-mysql_query-from-foreach/
Share on other sites

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.

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

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);

 

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.