我想将数据从 xml-feed 插入 mysql 数据库(见下面的代码)。
<?php
error_reporting(E_ALL); ini_set('display_errors', 'On');
set_time_limit(0);
$xmlDoc = new DOMDocument();
$xmlDoc->load("daisybelfeed.xml");
$mysql_hostname = "********";
$mysql_user = "*********";
$mysql_password = "*********";
$mysql_database = "*********";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd)
or die("Opps some thing went wrong");
$drop_tabel = "DROP TABLE belvilla";
$create_tabel = "CREATE TABLE belvilla
(title VARCHAR(255), img_medium TEXT, country_of_destination TEXT, region_of_destination TEXT,city_of_destination CHAR(100), latitude FLOAT( 10, 6 ) NOT NULL, longitude FLOAT( 10, 6 ) NOT NULL, link TEXT, image TEXT, minimum_price DECIMAL NOT NULL, maximum_price DECIMAL NOT NULL, stars NUMERIC(1) NOT NULL, max_nr_people NUMERIC(1) NOT NULL);";
$drop = mysql_query($drop_tabel) or die('Error, drop query failed');
$create = mysql_query($create_tabel) or die('Error, create query failed');
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=15000; $i++)
{
$title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$image=$x->item($i)->getElementsByTagName('img_medium')
->item(0)->childNodes->item(0)->nodeValue;
$country=$x->item($i)->getElementsByTagName('country_of_destination')
->item(0)->childNodes->item(0)->nodeValue;
$regio=$x->item($i)->getElementsByTagName('region_of_destination')
->item(0)->childNodes->item(0)->nodeValue;
$city=$x->item($i)->getElementsByTagName('city_of_destination')
->item(0)->childNodes->item(0)->nodeValue;
$latitude=$x->item($i)->getElementsByTagName('latitude')
->item(0)->childNodes->item(0)->nodeValue;
$longitude=$x->item($i)->getElementsByTagName('longitude')
->item(0)->childNodes->item(0)->nodeValue;
$link=$x->item($i)->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$vanafprijs=$x->item($i)->getElementsByTagName('minimum_price')
->item(0)->childNodes->item(0)->nodeValue;
$maximaalprijs=$x->item($i)->getElementsByTagName('maximum_price')
->item(0)->childNodes->item(0)->nodeValue;
$sterren=$x->item($i)->getElementsByTagName('stars')
->item(0)->childNodes->item(0)->nodeValue;
$maxpeople=$x->item($i)->getElementsByTagName('max_nr_people')
->item(0)->childNodes->item(0)->nodeValue;
$title=mysql_real_escape_string($title);
$image=mysql_real_escape_string($image);
$link=mysql_real_escape_string($link);
$regio=mysql_real_escape_string($regio);
$city=mysql_real_escape_string($city);
if ($country == 'FR'){
$insert = "INSERT INTO belvilla (title, img_medium, country_of_destination, region_of_destination, city_of_destination, latitude, longitude, link, image, minimum_price, maximum_price, stars, max_nr_people) VALUES ('$title', '$image', '$country', '$regio', '$city' $latitude, $longitude, '$link', '$image', $vanafprijs, $maximaalprijs, $sterren, $maxpeople)";
$add_member = mysql_query($insert) or die("Error, insert query failed Error: (" . mysql_errno() . ") " . mysql_error());
}}
?>
一切正常,除了一个问题:我收到一个 mySQL 错误代码 1064,这是由一些包含连字符的城市名称(如“La Roche-en-Ardenne”)引起的。正如您在代码中看到的那样,我尝试使用 mysql_real_escape_string 解决这个问题,但这并没有成功。
任何人都知道如何解决这个问题?
谢谢!