gpt4 book ai didi

php - 通过 php 的 SQL 语法错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:29 24 4
gpt4 key购买 nike

我是 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));
}

}
/*

最佳答案

你的意思是:

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

关于php - 通过 php 的 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18158142/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com