0

所以,我的 MySQL 查询有问题。它只显示来自变量的数据列表的第一个结果。这是我所拥有的

$Data='1,2,3'

$fetch = mysql_query("SELECT email FROM data WHERE uid IN ($Data) ");

if (mysql_num_rows($fetch)) {
    $emaildata = mysql_fetch_assoc($fetch);
    foreach($emaildata as $fetch2){
        echo $fetch2;
    }
} else {
    echo Failed;
}

现在,它只提取 3 的第一个结果,而不是 3 的每个单独的电子邮件。任何帮助表示赞赏,这对整个事情来说有点新。

4

3 回答 3

3

Use as below because you have iterate $fetch through while loop which is easier

while($emaildata = mysql_fetch_assoc($fetch)){
   echo  $emaildata['email']
}

Recommendations:

1.Learn to prevent from MySQL Injections: Good Link

2.Mysql extension is not recommended for writing new code. Instead, either the mysqli or PDO_MySQL extension should be used. More reading: PHP Manual

于 2012-11-25T16:25:34.670 回答
3

Fetch array instead of fetching associative, and use mysqli_() instead of mysql_() which is no longer maintained by the community, also use while loop instead of foreach and use index name if any

while($emaildata = mysql_fetch_array($fetch)) {
     echo $emaildata; //Use index name if any
   }
}
于 2012-11-25T16:26:33.517 回答
1

您应该使用 while 循环,只要有下一行,您就可以进行迭代:

while($row = mysqli_fetch_row($fetch)){
      echo  $row['email']
 }

注意!!!:mysqli_fetch_rowmysql_fetch_row推荐使用

于 2012-11-25T16:30:07.597 回答