I have a php script that stores match data (and other data), to a database. I had previously had it running through the xml file with a foreach, and storing/updating each record 1 at a time.
Seeing as there are over 3,000 match records, when updating, it would take over 45 seconds to complete. So I am trying to do it by using one query, rather than thousands of individual queries.
$updateQuery = "";
$data = simplexml_load_file('$url');
foreach($data->match as $match) {
$matchId = intval($match->id);
$homeTeamName = $match->homeTeamName;
$homeTeamScore = intval($match->homeTeamScore);
$awayTeamScore = intval($match->awayTeamScore);
$awayTeamName = $match->awayTeamName;
// Check if record exists
$query = "SELECT `matchId` FROM `matches` WHERE `matchId` = " . $matchId;
$result = mysqli_query($mysqli,$query);
if(mysqli_num_rows($result) == 1 ){
$updateQuery .= "UPDATE `matches`
SET
`homeTeamName`='" . $homeTeamName . "',
`homeTeamScore`=" . $homeTeamScore . ",
`awayTeamScore`=" . $awayTeamScore . ",
`awayTeamName`='" . $awayTeamName . "'
WHERE `matchId`=" . $matchId . "; ";
} else {
$sql_statement="INSERT INTO `matches`
(`matchId`, `homeTeamName`, `homeTeamScore`, `awayTeamScore`, `awayTeamName`)
VALUES
(" . $matchId . ",
'" . $homeTeamName . "',
" . $homeTeamScore . ",
" . $awayTeamScore . ",
'" . $awayTeamName . "')";
mysqli_query($mysqli,$sql_statement);
}
}
mysqli_query($mysqli,$updateQuery);
Please ignore the two types of queries (update/insert), once I get the update working, I will be changing the insert query to work similarly.
What I am finding is that when I view the result of $updateQuery, I can manually select it, place it into the phpmyadmin sql entry, and it works, it updates thge records, so the query being created isn't the issue (snippet below). But when I run the php script, the database doesn't get updated.
Snippet of the query generated:
UPDATE matches
SET homeTeamName
='Manchester_United', homeTeamScore
=2, awayTeamScore
=1, awayTeamName
='Leicester_City' WHERE matchId
=111981; UPDATE matches
SET homeTeamName
='AFC_Bournemouth', homeTeamScore
=2, awayTeamScore
=0, awayTeamName
='Cardiff_City' WHERE matchId
=108933;
What am I missing?