为什么我从MySQL查询中获取重复项?

The*_*lDK 2 php mysql sql ajax

我正在使用对PHP文件的Ajax调用来从MySQL数据库获取数据并在HTML中填充选择选项.问题是选项中的重复项目,我不知道为什么.我在工作台中尝试了查询,它带回了我需要的东西.

PHP文件:

<?php
    $q=$_GET["q"];

   // open db connection code

   $query = "select * from r2rtool.materialtype where type = 'FE' and tools like '%".$q."%'";
   $result = mysql_query($query);

   $option = "";

   while($row = mysql_fetch_array($result))
    {
        $mat = $row["Material"];
        $option.="<option value=\"$mat\">".$mat."</option>";

        echo $option;
    }

   // close db connection
?>
Run Code Online (Sandbox Code Playgroud)

Ajax功能:

function populatematerial(str)
{
    if (str=="") {
      document.getElementById("txtHint").innerHTML="";
      return;
    }

    if (window.XMLHttpRequest) {
      // IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }

    else{
      // IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200) {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
      }
    }

    xmlhttp.open("GET","phpfile.php?q="+str,true);
    xmlhttp.send();
}
Run Code Online (Sandbox Code Playgroud)

Dej*_*vic 6

while($row = mysql_fetch_assoc($result))
{
     $option .= "<option value=\"{$row[Material]}\">{$row[Material]}</option>";
}
echo $option;
Run Code Online (Sandbox Code Playgroud)