mysql-名称在数组中的位置

时间:2018-09-19 08:38:12

标签: php mysql

   $CustomerName= preg_replace('/[^a-zA-Z0-9-_\.,& ]/','',$row["Customer_Name"]);
   array_push($CustomerName2,$CustomerName);
   $sqlquery = "SELECT * FROM Customer where Customer_Name IN ($CustomerName2)";
   $stmt = mysql_query($conn,$sqlquery);
   echo(print_r(sqlsrv_errors(), true));
   while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))    
   {    
       $sqlCustomerIdMaps[$row[0]]= $row[0];
   }

我对php不太满意,在上面的php代码中,从sql查询数据。我的输出来自数组$CustomerName2,例如:

Array ( [0] => Acme Janitor & Chemical Supply [1] => Adam Zampa [2] => AMAZON.COM PHOENIX PHX6 [3] => Aston Agar [4] => Austin Consulting Group [5] => Brad Hodge [6] => Cedrick Johnson [7] => Crowen [8] => David Miller [9] => David Warner [10] => DKR [11] => DKR [12] => DKR [13] => Jaworskyj Gabrielle [14] => Johan2 [15] => Jone [16] => Keith Olson [17] => Mallika [18] => Manoj Reddy [19] => Michael Bevan [20] => Philip Andry [21] => Shane1 Warne1 [22] => Spad Consulting Group [23] => Usman Khawaja [24] => Zohar )

在sql查询中,我没有收到任何错误,但查询无法正常工作?

我需要查询Customer_Name并添加结果以映射以更新sql server中的数据。

任何人都可以帮助我解决此问题。

谢谢!

1 个答案:

答案 0 :(得分:1)

您的问题是$CustomerName2是一个数组,您在查询时尝试将其用作字符串。将该代码更改为:

$sqlquery = "SELECT * 
    FROM Customer
    WHERE Customer_Name IN ('" . implode("','", $CustomerName2) . "')";

更新

您使用了错误的数据库库来获取数据。基于您正在使用mysqli库连接到MySQL数据库这一事实,您应该将代码的最后一部分重写为:

$result = mysqli_query($conn, $sqlquery);
echo mysqli_errno($conn);
while ($row = mysqli_fetch_array($result) {
    $sqlCustomerIdMaps[$row[0]]= $row[0];
}