在创建统计程序时,我会跟踪每小时有多少架飞机到达机场。
我的脚本计算过去一小时的航班数量并将其保存到数据库中。如果我在phpmyadmin中复制语句,它会继续获取mysql语法错误
$date = 2013091713;
//$date = date('YmdH', mktime(date('H')));
$sql = '';
foreach ($aerodrome as $icao => $value) {
$sql .= "INSERT INTO flightsperhour(date, aerodrome, inbound, outbound)
VALUES('". $date ."', '" . $icao . "' , " . (isset($value['inboud']) ? $value['inboud'] : 0) . ", " . (isset($value['outbound']) ? $value['outbound'] : 0) . ");";
}
//INSERT INTO flightsperhour(date, aerodrome, inbound,outbound) VALUES('2013091713', 'EBBR' , 0, 1);
//INSERT INTO flightsperhour(date, aerodrome, inbound,outbound) VALUES('2013091713', 'ELLX' , 0, 1);
if(mysql_query($sql))
{
} else{
die(mysql_error());
}
编辑:Mysql错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO flightsperhour(date, aerodrome, inbound, outbound) VALUES('2013' at line 2
EDIT2:修复了单个缺失的'
EDIT3:根据编程学生的要求,他的解决方案的print_r
INSERT INTO flightsperhour(date, aerodrome, inbound, outbound)
VALUES('2013091713', 'EBBR' , '', '1'),
VALUES('2013091713', 'ELLX' , '', '1')
You have an error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use
near 'VALUES('2013091713', 'ELLX' , '', '1')' at line 3
答案 0 :(得分:0)
'
和$date
之间缺少冒号$icao
。
更正后的代码:
$date = 2013091713;
//$date = date('YmdH', mktime(date('H')));
$sql = '';
$sql .= "INSERT INTO flightsperhour(date, aerodrome, inbound, outbound) ";
foreach ($aerodrome as $icao => $value) {
$inbound = isset($value['inboud']) ? $value['inboud'] : 0;
$outbound = isset($value['outbound']) ? $value['outbound'] : 0;
$sql .= "
VALUES('". $date ."', '" . $icao . "' , '" . $inboud . "', '" . $outbound . "'),";
}
$sql = substr($sql, 0, strlen($sql)-1);
//INSERT INTO flightsperhour(date, aerodrome, inbound,outbound) VALUES('2013091713', 'EBBR' , 0, 1);
//INSERT INTO flightsperhour(date, aerodrome, inbound,outbound) VALUES('2013091713', 'ELLX' , 0, 1);
if(mysql_query($sql))
{
} else{
die(mysql_error());
}
答案 1 :(得分:0)
您尚未关闭$date
将此'". $date ."
更改为'". $date ."'
$sql .= "INSERT INTO flightsperhour(date, aerodrome, inbound, outbound)
VALUES('". $date ."', '" . $icao . "' , " . (isset($value['inboud']) ? $value['inboud'] : 0) . ", " . (isset($value['outbound']) ? $value['outbound'] : 0) . ");";
答案 2 :(得分:0)
建议您使用PDO http://in1.php.net/manual/en/ref.pdo-mysql.php或MySQLi Extension,因为Php 5.5不再支持MySQL扩展
$date = 2013091713;
//$date = date('YmdH', mktime(date('H')));
$db_conn = new PDO('mysql:host=<host>;dbname=<database name>', '<username>', '<password>');
try{
$sql = $db_conn->prepare("INSERT INTO flightsperhour(date, aerodrome, inbound, outbound) VALUES (:date,:aerodrome,:inbound,:outbound)");
foreach ($aerodrome as $icao => $value) {
$inbound = isset($value['inboud']) ? $value['inboud'] : 0;
$outbound = isset($value['outbound']) ? $value['outbound'] : 0;
$sql->execute(
array(
":date" => $date,
":aerodrome" => $aerodrome,
":inbound" => $inbound,
":outbound" => $outbound
)
);
}
}
catch (PDOException $pe){
print $pe->getMessage();
}