我已经构建了一个PHP / MySQL CSV上传器/更新程序来更新高中橄榄球队的分数。
当我使用Windows 7在XAMPP上运行此脚本时,我收到了“LOAD DATA INFILE”查询的权限错误。我在PHPMyAdmin中检查了我的权限,并且用户拥有所有权限,所以不是这样。
我需要先解决这个问题。我最终通过直接在PHPMyAdmin上运行查询来获取所有内容。但是,当我尝试运行两个上传查询时,我的脚本超时了。
我要在这里处理的过程是上传文件并将记录导入新表(csv_data)。然后我想运行两个更新查询以使用csv_data表中的新数据更新游戏表。
这两个查询的原因是因为有时错误的团队在家/访客的错误列中。我发现使用CONCAT()确实减慢了速度。
所以我需要首先使用“LOAD DATA INFILE”查询修复我的权限问题,然后找出加速更新查询的方法。有什么想法吗?
<?php
require_once('global.php');
if (array_key_exists('submit', $_POST)) {
if ($_FILES['csv']['error'] > 0){
echo "Error: " . $_FILES['csv']['error'] . "</br>" . "You have not selected a file or there is another error.";
} else {
$tmp = $_FILES['csv']['tmp_name'];
}
if (!$_FILES['csv']['type'] == 'text/csv'){
echo "Please select a CSV File";
} else {
$location = dirname(__FILE__) . '/csv/' . basename($_FILES['csv']['name']);
move_uploaded_file($tmp, $location);
}
$errors = array();
// Need to get this query to work properly regarding permissions
$query = $db->prepare("LOAD DATA INFILE ?
INTO TABLE csv_data
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(game_week, home_team, home_score, visitor_team, visitor_score)");
if ($query->execute(array($location))) {
try {
// Need to figure out how to use LIKE instead of = and do it quickly
$query_two = $db->prepare("UPDATE game g, csv_data c, team home_team, team visit_team
SET g.game_home_score = c.home_score,
g.game_visitor_score = c.visitor_score,
g.game_complete = 'Y'
WHERE g.game_week = c.game_week
AND home_team.team_name LIKE CONCAT('%', c.home_team, '%')
AND home_team.team_id LIKE CONCAT('%', g.game_home_team, '%')
AND visit_team.team_name LIKE CONCAT('%', c.visitor_team, '%')
AND visit_team.team_id LIKE CONCAT('%', g.game_visitor_team, '%')");
if (!$query_two->execute()) {
$errors[] = 'The first update query failed.';
}
$query_three = $db->prepare("UPDATE game g, csv_data c, team home_team, team visit_team
SET g.game_home_score = c.visitor_score,
g.game_visitor_score = c.home_score,
g.game_complete = 'Y'
WHERE g.game_week = c.game_week
AND home_team.team_name LIKE CONCAT('%', c.visitor_team, '%')
AND home_team.team_id LIKE CONCAT('%', g.game_home_team, '%')
AND visit_team.team_name LIKE CONCAT('%', c.home_team, '%')
AND visit_team.team_id LIKE CONCAT('%', g.game_visitor_team, '%')");
if (!$query_three->execute()) {
$errors[] = 'The second update query failed.';
}
} catch(PDOException $e) {
echo $e->getMessage();
}
} else {
$errors[] = 'CSV Upload Query Failed.';
}
}
?>