1

值 startlat,startlon,endlat,endlon 在我的查询字符串中显示为来自 simplexml 对象的字段名称。

我打印出查询字符串,查看第 3、4、5 和 6 个值

insert into *database.table* (mwayname, sectionname, startlat, startlon,   endlat,endlon,avgspd,avgocc,totvol,seclength,sectime)VALUES ('AKL-SH1-NB-RNM' , '1-NB  PAPAKURA OFF to PAPAKURA ON' , '->latitude', '->longitude' , '->latitude' , '->longitude' ,'98.7012505914656' , '3.0' , '12.0' , '0.309545' , '0.18817086803564703')

它将 simplexml-> 作为文本发送到查询中,但其他值(包括数字)不受影响?当然不是因为查询构造函数在循环中吗?

$xml = new SimpleXMLElement($response);

foreach($xml->return as $return)
{ 
echo '**********************',$return->name.'*****<br>';
echo '**********************',$return->reference.'*****<br>';

foreach($return->segments as $segments)
{
echo $return->name.'<br>';
echo $return->reference.'<br>';

echo $segments->carriagewaySegmentId.' ID <br>';
echo 'section name ',$segments->sectionName.'<br>';

echo 'start lat ',$segments->startLocation->latitude.'<br>';
echo 'start lng ',$segments->startLocation->longitude.' <br>';

echo 'end lat ',$segments->endLocation->latitude.'<br>';
echo 'end long ',$segments->endLocation->longitude.'<br>';

echo 'avg speed = ',$segments->averageSpeed. ' kmh avg<br>';
echo $segments->averageOccupancy. ' % of rd occupied<br>';
echo $segments->totalVolume. ' traffic volume<br>';
echo $segments->sectionLength. ' km<br>';
echo $segments->sectionTime. ' minutes<br>';
echo'<br>';
//send to mysql
$query = "insert into trafficstats.master (mwayname, sectionname, startlat, startlon,     endlat,endlon,avgspd,avgocc,totvol,seclength,sectime)VALUES ('$return->reference' ,   '$segments->sectionName' , '$segments->startLocation->latitude', '$segments->startLocation->longitude' ,  '$segments->endLocation->latitude' , '$segments->endLocation->longitude' ,'$segments- >averageSpeed' , '$segments-

>averageOccupancy' , '$segments->totalVolume' , '$segments->sectionLength' ,  '$segments->sectionTime')";
echo $query;
mysql_query($query);
}
}
?>
4

2 回答 2

2

试试这个查询

$query = "insert into trafficstats.master (mwayname, sectionname, startlat, startlon,     endlat,endlon,avgspd,avgocc,totvol,seclength,sectime)
VALUES ('".$return->reference."' ,   '".$segments->sectionName."' , 
'".$segments->startLocation->latitude."', '".$segments->startLocation->longitude."' ,  
'".$segments->endLocation->latitude."' , '".$segments->endLocation->longitude."' ,
'".$segments- >averageSpeed."' , '".$segments->averageOccupancy."' , 
'".$segments->totalVolume."' , '".$segments->sectionLength."' ,  
'".$segments->sectionTime."')";
于 2013-03-28T06:54:22.900 回答
1

为了解释这里的问题,当 PHP 遇到双引号字符串时"something $foo->bar->baz something",它假设您只想替换$foo->bar,而不是整个$foo->bar->baz.

解决方案是(正如其他人指出的那样)将多个部分的字符串连接在一起('something ' . $foo->bar->baz . ' something')或将要替换的变量括在大括号中,以便 PHP 知道它都是一个变量("something {$foo->bar->baz} something")。

有关此语法的更多信息,请参阅PHP 手册的相关部分。

于 2013-03-28T20:13:45.900 回答