Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an issue where i have a textarea in a form where users can enter names on seperate lines. On submit I explode the "\n".

I then want to pass the values from the array into an Select statement, but when i run the script it only returns one result (the last one) from the array..

here is the code below.

echo "<h1> You searched for the following names </h1>";

include 'conn.php';
mysql_select_db("email_finder", $con);
$Email = $_POST['EmailBox'];
$str = $Email;  
$lines = explode("\n", $str);
//$in = implode(',', $lines); 
//$userStr = implode(',', $lines);


 echo "<table border='0'>
<tr>
<th style='color:White' width='180px'; bgcolor=#999999>Name</th>
<th style='color:White' width='250px'; bgcolor=#999999>Email</th>
</tr>";

echo "<pre>";
print_r($lines);
echo "</pre>";

foreach($lines as $array_element) { 

$result = mysql_query("SELECT * FROM `emails` WHERE `Name` IN('$array_element') ORDER BY `LastName`");

echo "<pre>";
print_r($array_element);
echo "</pre>";

while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { 

echo "<tr>";
echo "<td style='padding-left:5px'><b> ".$row['Name']."</b>&nbsp:&nbsp</td>";
printf("<td style='padding-left:5px'><a href=mailto:" .$row['Email'].  ">"  .$row['Email']. "</a></td>");
echo "</tr>";   

echo "<pre>";
print_r($row);
echo "</pre>";


}
}
echo "</table><br />";

echo "Email <b>ALL</b> these Students: <a href=mailto:".$row['Email']." >Click Here</a> <br />";

mysql_close($con);


echo '<br />';

If you can help i would be greatful

Thanks

share|improve this question
1  
You're running a query for each element in the array instead of one query that fetches all rows in one go. – Mark Byers Mar 22 '12 at 20:11
2  
You have only a 33% accept rate, did you accept all correct answers on your previous questions? – Matthieu Napoli Mar 22 '12 at 20:13
create an array of $result too. – hjpotter92 Mar 22 '12 at 20:14
Your $lines = explode("\n", $str); line may be leaving \r at the end of each row. Maybe try $lines = preg_split("/\\r\\n|\\r|\\n", $str); – AndrewR Mar 22 '12 at 20:17
1  
@Matthieu It worked - 83% :) – AndrewR Mar 22 '12 at 20:22
show 2 more commentsadd comment (requires an account with 50 reputation)

2 Answers

up vote 1 down vote accepted

Guessing you've got OS specific line endings tripping you up. Try preg_split

$lines = preg_split("/\\n|\\r|\\r\\n/", $str);
share|improve this answer
Hmm missed the Query Error. Ben's solution is most definitely more likely the culprit, but you may need your line endings fixed as well. – Andrew Edvalson Mar 22 '12 at 20:23
You beauty!!! that worked thanks – SteveU Mar 22 '12 at 20:33
add comment (requires an account with 50 reputation)

Try using equals instead of IN:

$result = mysql_query("SELECT * FROM emails WHERE Name='$array_element' ORDER BY LastName");

Otherwise my advise to debug it would be to put an exit(); statement on your first pass of the while loop to check your array value and/or if you get a result of the first entry. something like:

foreach($lines as $array_element) {
    $result = mysql_query("SELECT * FROM `emails` WHERE `Name`='$array_element' ORDER BY `LastName`");

    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
    print_r($row);
    exit;
    ...
share|improve this answer
And sanitize the input while you are at it – papirtiger Mar 22 '12 at 20:27
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.