Break out of loop if value is encountered?
Posted: Fri Apr 16, 2004 8:59 pm
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:
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.

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