Basically I have a php script which calls an external XML file
it then inserts certain data from the xml file into the mysql db inside a foreach loop
However when I check the database its empty
What am I doing wrong
The xml contains the following structure
<jobs>
<job>
<jobref>987654321</jobref>
<title>TITLE HERE</title>
<url>
URL HERE
</url>
<salary>£85 - £105/day benifits</salary>
<location>LOCATION</location>
<description>desc</description>
<category>CAT HERE</category>
</job>
<job>
<jobref>123456789</jobref>
<titleTITLE HERE</title>
<url>
URL HERE
</url>
<salary>£85 - £105/day benifits</salary>
<location>LOCATION</location>
<description>desc</description>
<category>CAT</category>
</job>
</jobs>
Below is the PHP code
<?php
$dbu = 'user';
$dbp = 'pass';
$dbh = 'localhost';
$dbn = 'dbname';
$dbc = mysql_pconnect($dbh,$dbu,$dbp);
// Get XML Feed
$startPage = $_GET['page'];
$perPage = 3000;
$currentRecord = 0;
$xml = simplexml_load_file("urlhere");
$jobs = $xml->xpath("/jobs/job");
foreach ($jobs as $job)
{
mysql_select_db($dbn,$dbc);
$query = mysql_query("INSERT INTO `listings` (`jobref`,`title`,`url`,`salary`,`location`,`description`,`category`) VALUES ('$job->jobref','$job->title','$job->salary','$job->location','$job->description','$job->category')",$dbc);
echo "$job->jobref Added <br>";
}
?>
It displays :
120239811 Added
201909016 Added
202482907 Added
when i run the script so its parsing the xml file but its not actually inserting the data
Any help would be great because I'm on a very tight timeframe to finish this Thanks in advance
** EDIT BELOW ** NEW code below
<?php
$startPage = 1;
$perPage = 3000;
$currentRecord = 0;
$xml = simplexml_load_file("urlhere");
$jobs = $xml->xpath("/jobs/job");
mysql_select_db($dbn,$dbc);
foreach ($jobs as $job)
{
$currentRecord += 1;
if($currentRecord > ($startPage * $perPage) && $currentRecord < ($startPage * $perPage + $perPage)){
$query = mysql_query("INSERT IGNORE INTO `listings` (jobref,title,url,salary,location,description,category) VALUES ('$job->jobref','$job->title','$job->url','$job->salary','$job->location','$job->description','$job->category')",$dbc);
if($query){
echo "$job->jobref Added <br>";
} else {
echo mysql_error();
}
}
}
?>
Its now just adding the 1st entry in the XML file
Removed IGNORE from query and added ON DUPLICATE KEY UPDATE jobref =$job->jobref+1 still same problem
did you sure the xml loaded succesfully? :) but i guess from your sample the xml node <salary>
has illegal ampersand char "&"
here the link about it .
but you can deal with it with a little hack like this .
i suggest you to use mysqli
, so according to your xml sample
...
$conn = mysqli_connect($dbh, $dbu, $dbp);
if(!$conn) die('connect fail: '. mysqli_error($conn));
$db = mysqli_select_db($conn, $dbn);
if(!$db) die('db fail: '. mysqli_error($conn));
...
$file = file_get_contents(url_here);
// replace '&' to '&'
$file = str_replace('&', '&', $file);
$xml = simplexml_load_string($file);
$jobs = $xml->xpath("/jobs/job");
foreach ($jobs as $job)
{
...
// just get back those ampersand
$job->salary = str_replace('&', '&', $job->salary);
$s = "INSERT IGNORE INTO `listings` (`jobref`,`title`,`url`,`salary`,`location`,`description`,`category`) VALUES('$job->jobref','$job->title','$job->url','$job->salary','$job->location','$job->description','$job->category')";
$q = mysqli_query($conn, $s);
if(!$q) {
die(mysqli_error($conn));
}else {
echo "$job->jobref Added <br>";
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.