Page 1 of 1

Break out of loop if value is encountered?

Posted: Fri Apr 16, 2004 8:59 pm
by Steveo31
I'm inserting a bunch of actors/actresses to a DB by splitting the input with the delimiter "|". Then it inserts to the DB like so:

Code: Select all

<?php

mysql_connect('localhost', 'root');
mysql_select_db('people');

$actors = "John Wayne | Bruce Lee | Steven Segall | Tim Meadows";

$indiv = explode("|", $actors);
foreach($indiv as $key=>$value){
   echo $key.':  '.$value.'<br />';

   $get = "SELECT name FROM people_table";
   $get_query = mysql_query($get);
#######
### Not working:
#######
			while($row = mysql_fetch_assoc($get_query)){
			    if($value == $row['name']){
			       break;
			    }
			}
## end "not working"
			$sql = "INSERT INTO `people_table` (`name`) VALUES ('$value')";
			$query = mysql_query($sql) or die(mysql_error());

?>
So yea.. the "while" part is what isn't working. Basically I need it to stop the loop, thus not inserting the name if it is found in the database.

:?

Posted: Fri Apr 16, 2004 9:31 pm
by feyd
change

Code: Select all

$indiv = explode("|", $actors);
to

Code: Select all

$indiv = explode(" | ", $actors);
should take care of it.

Posted: Fri Apr 16, 2004 10:45 pm
by Steveo31
That... or should I trim() them?

Thanks for the reply though.