我想将日期介于“最后一个星期二”和“今天”之间的特定列的值相加。我写了这段代码:
<?php
define('DB_HOST', '****');
define('DB_USER', '****');
define('DB_PASSWORD', '****');
define('DB_DATABASE', '****');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Function to sanitize values received from the form. Prevents SQL injection
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
//Sanitize the POST values
$dk=strtotime("today");
$d=strtotime("previous Tuesday");
$res=mysql_query("SELECT sum(mball202) FROM manager WHERE dateid >= date('Y-m-d', $d) AND dateid <= date('Y-m-d', $dk)");
while($row=mysql_fetch_array($res))
{
?>
<tr>
<td align="center"><?php echo $row['sum(mball202)']; ?></td>
</tr>
<?php
}
?>
但如果我运行该文件,则返回(警告:mysql_fetch_array()期望参数1为资源,布尔值在/srv/disk11/1165502/www/tst.scienceontheweb.net/admin/manager/result-weekly中给出第44行的.php )
我做错了什么?请有人帮助我。
答案 0 :(得分:1)
mysql_error返回false,因为您查询时出错。你应该纠正它:
$dateStart = date('Y-m-d', $d);
$dateEnd = date('Y-m-d', $dk);
$res=mysql_query('SELECT sum(mball202) FROM manager WHERE dateid >= "' . $dateStart . '" AND dateid <= "' . $dateEnd . '"');
否则,您的查询是:
SELECT sum(mball202) FROM manager WHERE dateid >= date('Y-m-d', 1470693600) AND dateid <= date('Y-m-d', 1470780000)