Hi
I am running EasyPHP for testing and have set up a database called foxs which has no password and the table is called fgparts. I have a .csv file which I want to pass into it but nothing is loading into the sql.
The database is structured as follows:-
fgpartsID Integer
PARTNO_XRF VARCHAR(50)
PARTNO_PREF VARCHAR(50)
DESCRIPTION VARCHAR(50)
SHELFLIFE VARCHAR(50)
Example of Feed:
PARTNO_XRF PARTNO_PREF DESCRIPTION SHELFLIFE
X602 F500321 P&H COMPOSITE PACK (COOL BAG) 365
<?php
require("MagicParser.php");
mysql_connect("localhost","root","");
mysql_select_db("foxs");
function myRecordHandler($fgpart)
{
$sql = "
INSERT INTO 'fgparts' SET
PARTNO_XRF = '".mysql_real_escape_string( $fgpart["PARTNO_XRF"] )."',
PARTNO_PREF = '".mysql_real_escape_string( $fgpart["PARTNO_PREF"] )."',
DESCRIPTION = '".mysql_real_escape_string( $fgpart["DESCRIPTION"] )."',
SHELFLIFE = '".mysql_real_escape_string( $fgpart["SHELFLIFE"] )."',
";
mysql_query($sql);
}
mysql_query("DELETE FROM fgparts");
MagicParser_parse("ShelfLife.csv","myRecordHandler","csv|44|1|0");
?>
Any help would be appreciated.
Thanks
Roy
Hi Roy,
Not sure if you got the above to work so thought I would post a section of code that has worked for me.
<?php
require("../includes/magicparser.php");
mysql_connect("xxx", "xxx", "xxx") or die(mysql_error());
mysql_select_db("xxx") or die(mysql_error());
function myRecordHandler($hotel)
{
$sql = "INSERT INTO hotels (name,star,address,city,postcode,description,ref,link) VALUES ('".mysql_real_escape_string($hotel["HOTEL_NAME"])."',
'".mysql_real_escape_string($hotel["HOTEL_STAR"])."',
'".mysql_real_escape_string($hotel["HOTEL_ADDRESS"])."',
'".mysql_real_escape_string($hotel["HOTEL_CITY"])."',
'".mysql_real_escape_string($hotel["HOTEL_POSTCODE"])."',
'".mysql_real_escape_string($hotel["HOTEL_DESCRIPTION"])."',
'".mysql_real_escape_string($hotel["HOTEL_REF"])."',
'".mysql_real_escape_string($hotel["HOTEL_LINK"])."')";
mysql_query($sql);
echo "Data Inserted!";
}
$url = "http://xml.laterooms.com/xml_1.php3?pid=xxx&ctry=1&top=10";
MagicParser_parse($url,"myRecordHandler","xml|HOTEL_SEARCH/HOTEL/");
?>
Hope this helps,
Simon
Hi Roy,
There is an error in your SQL statement - it has a comma on the end (so it would end in SHELFLIFE='something', ). I think the line where you construct the SQL should be:
<?php
$sql = "
INSERT INTO 'fgparts' SET
PARTNO_XRF = '".mysql_real_escape_string( $fgpart["PARTNO_XRF"] )."',
PARTNO_PREF = '".mysql_real_escape_string( $fgpart["PARTNO_PREF"] )."',
DESCRIPTION = '".mysql_real_escape_string( $fgpart["DESCRIPTION"] )."',
SHELFLIFE = '".mysql_real_escape_string( $fgpart["SHELFLIFE"] )."'
";
?>
Hope this helps...
Cheers,
David.