PHP Array into mysql select Statement

时间:2012-03-22 20:09:03

标签: php mysql arrays select textarea

我有一个问题,我有一个表单中的textarea,用户可以在单独的行中输入名称。提交时我会爆炸“\ n”。

然后我想将数组中的值传递给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>&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 />';

如果你能帮助我会很棒

由于

2 个答案:

答案 0 :(得分:3)

尝试使用equals而不是IN:

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

否则我建议调试它就是放一个exit();第一次传递while循环时的语句,用于检查数组值和/或是否获得第一个条目的结果。类似的东西:

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

答案 1 :(得分:1)

猜猜你有特定于操作系统的行结尾绊倒了你。试试preg_split

$lines = preg_split("/\\n|\\r|\\r\\n/", $str);