我有一个问题,我有一个在用户可以在单独的行上输入名称的形式的textarea。在提交时,我将爆炸“\ n”。PHP Array into mysql select语句
然后,我想从数组中的值传递到一个Select语句,但是当我运行该脚本,它只返回从数组..
这里是下面的代码一个结果(最后一个)。
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> : </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 />';
如果你能帮助我将不胜感激
感谢
您正在为数组中的每个元素运行查询,而不是一次性提取所有行的查询。 – 2012-03-22 20:11:48
也创建'$ result'数组。 – hjpotter92 2012-03-22 20:14:06
你的'$ lines = explode(“\ n”,$ str);'行可能会在每行的末尾留下'\ r'。也许试试''$ lines = preg_split(“/ \\ r \\ n | \\ r | \\ n”,$ str);' – AndrewR 2012-03-22 20:17:55