无法从Simplexml发送lat / lng到mysql数据库

时间:2013-03-28 06:47:05

标签: php mysql simplexml

值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);
}
}
?>

2 个答案:

答案 0 :(得分: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."')";

答案 1 :(得分:1)

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

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

有关此语法的详情,请参阅the relevant section of the PHP manual