0

我对php有点陌生,到目前为止,这个地方对我帮助很大!无论如何,我有这个代码

$month = "
SELECT SUM(`duration`) 
FROM `connlist` AS `month_sum` 
WHERE `vatsimid` = '$vatsimid' 
AND MONTH(atc_online) = " . $pmonth . "  
AND YEAR(atc_online) = " . $year . "
"; 

这就是我回显 $month 时得到的结果

SELECT SUM(`duration`) 
FROM `connlist` AS `month_sum`
WHERE `vatsimid` = '1070757' 
AND MONTH(atc_online) = 07 
AND YEAR(atc_online) = 13

当我直接在 phpMyAdmin 中使用它时,它就像一个魅力,但是当我尝试通过一个 php 网页进行操作时,我得到了语法错误。我正在使用 php 5.4

谢谢!

编辑:完整代码:

<?php
//open MySQL connection
$mysql = mysqli_connect('host', 'un', 'pass', 'table') 
or die ( "MySQL Error: ".mysqli_error() );

//Get and decode residents data
$jsonData = file_get_contents("link");
$phpArray = json_decode($jsonData, true);

//Start Operations
foreach ($phpArray as $key => $value) {

//Get controller hours for today
$vatsimid = $value[vatsimid];

//Get previous month
$pmonth = date("m", strtotime("-35 days") ) ;
$pmonthName = date("M", strtotime("-35 days") ) ;
echo $pmonth;
echo $pmonthName;


//This year or last year? 
If (date("M") != "Jan") { //Checks that it's not January of the next year.
    $year = date("y");
}
else {
    $year = date("y", strtotime("-1 month") );
}

echo $year;

//Search and sum entries during last month
$month = "SELECT SUM(`duration`) 
FROM `connlist` AS `month_sum` 
WHERE `vatsimid` = '$vatsimid' 
AND MONTH(atc_online) = " . $pmonth . " 
AND YEAR(atc_online) = " . $year . "";
echo $month;
echo "</br> </br>";

$result = mysqli_query($mysql,$month);
$row = mysqli_fetch_assoc($result); 

$month_sum = $row['month_sum'];
echo $month_sum;

//Updates data in atclist 

$datainsert = "
UPDATE `atclist`
SET " . $monthName . "=" . $month_sum . "
WHERE vatsimid = " . $vatsimid . "";
$insert = mysqli_query($mysql,$datainsert);

if (!$insert)
{
die('Error: ' . mysqli_error($mysql));
}  

}
/*
4

2 回答 2

3

你的意思:

SELECT SUM(duration) AS month_sum 
FROM connlist 
WHERE vatsimid = '1070757' AND MONTH(atc_online) = 07 AND YEAR(atc_online) = 13
于 2013-08-10T02:37:06.700 回答
1

看起来$month_sum变量在您的 UPDATE 查询中未设置或为空。

您可以添加单引号,例如

$datainsert = "
    UPDATE atclist
    SET ".$monthName."= '".$month_sum."' 
    WHERE vatsimid= '".$vatsimid."'";
于 2013-08-10T05:18:39.577 回答