php中数据加载中的数组到字符串转换
Posted
技术标签:
【中文标题】php中数据加载中的数组到字符串转换【英文标题】:Array to string conversion in php in data loading 【发布时间】:2018-04-17 09:50:16 【问题描述】: <?php
$serverName = "RAHULTCO-PC\MANTRA2005";
$url1=$_SERVER['REQUEST_URI'];
header("Refresh: 1; URL=$url1");
$connectionInfo = array("Database"=>"PayTime", "UID"=>"sa", "PWD"=>"Mantra123");
$conn = sqlsrv_connect($serverName, $connectionInfo);
$con1 = $con1=mysqli_connect('localhost','root','','gps_db');
$date = date('Y-m-d H:i:sa');
$sql = "SELECT TOP 10 CardNO FROM dbo.tmpDmpTerminalData order by In_Out_Time desc ";
/*$sql = "SELECT [CardNO]
FROM [PayTime].[dbo].[tmpDmpTerminalData] WHERE (IPaddress = '172.23.42.141' AND In_Out_Time >= '$date')";*/
$stmt = sqlsrv_query( $conn, $sql);
if( $stmt === false )
die( print_r( sqlsrv_errors(), true));
// Make the first (and in this case, only) row of the result set available for reading.
if( sqlsrv_fetch( $stmt ) === false)
die( print_r( sqlsrv_errors(), true));
// Get the row fields. Field indeces start at 0 and must be retrieved in order.
// Retrieving row fields by name is not supported by sqlsrv_get_field.
if ($conn)
$items = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) )
$items[] = $row;
print_r($items);
else
echo "connection failed.<br />";
die (print_r(sqlsrv_errors(), true));
$host = "localhost";
$user = "root";
$pass = "";
$db = "gps_db";
$con = mysqli_connect($host, $user, $pass, $db);
if ($con)
echo "Connected";
$query = "SELECT `user_id` FROM `gps_gatepassmaster` WHERE `punch_id` IN (".implode(',',$items).")";
$run = mysqli_query($con, $query);
if ($run == TRUE)
?>
<table border="1" align="center">
<tr>
<td>user_id</td>
</tr>
<?php
while($data= mysqli_fetch_assoc($run))
?>
<tr>
<td><?php echo $data['user_id'];?></td>
</tr>
<?php
?></table><?php
数组的输出 = Array ( [0] => Array ( [CardNO] => 115293 ) [1] => 数组 ( [CardNO] => 100087 ) [2] => 数组 ( [CardNO] => 115293 ) [3] => 数组 ( [CardNO] => 100087 ) [4] => 数组 ( [CardNO] => 115293 ) [5] => 数组 ( [CardNO] => 115163 ) [6] => 数组 ( [CardNO] = > 115201 ) [7] => 数组 ( [CardNO] => 115293 ) [8] => 数组 ( [CardNO] => 115163))
数据未加载出现错误
"数组到字符串的转换 D:\wamp\www\reports_vinay\connect_to_multiple.php 第 54 行"。
【问题讨论】:
您使用的是 MySQL 还是 MS SQL Server? @jarlhsqlsrv_connect()
建议他在sql server上
我不知道第 54 行包含什么...
请以后只选择与您的问题相关的标签,不要向其他标签发送垃圾邮件
【参考方案1】:
implode
仅适用于单维数组,您传递的是多维数组。您应该可以通过只需将卡号放入$items
数组而不是整行来解决它。
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) )
$items[] = $row['CardNO'];
【讨论】:
【参考方案2】:if ($conn)
$items = array();
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) )
$items[] = $row;
print_r($items);
这意味着您的 $items
是一个关联数组,位于第 54 行。
那是WHERE
punch_idIN (".implode(',',$items).")";
,在这里你将它用作简单的数组。
这就是问题所在。
将$items[] = $row;
更改为$items[] = $row['CardNO'];
【讨论】:
以上是关于php中数据加载中的数组到字符串转换的主要内容,如果未能解决你的问题,请参考以下文章