-3

我在 PHP 中使用 simple_load_file 读取 XML 标记并导入 XML 文件数据库发生错误

如何使用php将XML文件导入数据库

ini_set('display_errors','On');

echo "starting";

//mysql connection
$con2 = mysql_connect("localhost","root","root");
if (!$con2)  {  
    die('Could not connect: ' . mysql_error());  
}

$selectdb = mysql_select_db("weberpdemo", $con2);
if (!$selectdb)  { 
    die('Database not used: ; ' . mysql_error());  
}

echo "connected to DB<br /><br />";



    examples given $mess = simplexml_load_file($filename);
echo "xml loaded<br /><br />";

$messageid = mysql_real_escape_string($mess->messageid);
$mobile = mysql_real_escape_string($mess->mobile);
$time = mysql_real_escape_string($mess->time);
$latitude = mysql_real_escape_string($mess->latitude);
$longitude = mysql_real_escape_string($mess->longitude);
$status = mysql_real_escape_string($mess->status);
$speed = mysql_real_escape_string($mess->speed);
$address = mysql_real_escape_string($mess->address);
$direction = mysql_real_escape_string($mess->direction);
$runningodo = mysql_real_escape_string($mess->runningodo);

echo "xml parsed<br /><br />";

//insert into databse                     
mysql_query("INSERT INTO xml (messageid, mobile, time, latitude, longitude, status, speed, address, direction, odometer)
VALUES ('$messageid', '$mobile', '$time', '$latitude', '$longitude', '$status', '$speed', '$address', '$direction', '$runningodo')")
or die(mysql_error());
4

1 回答 1

2

取决于您在导入数据库后尝试与数据一起归档的内容。

  • 如果只想保存数据并按原样提取,可以在保存前序列化数据。
  • 如果需要索引或直接访问存储的 XML 数据,则应使用 load_xml。
于 2013-05-14T07:15:23.257 回答