doulu4976 2016-08-10 15:44
浏览 39
已采纳

我发现很难使用sum()

I want to sum the values of a particular column where the date is between the "last Tuesday" and "Today". I wrote this code:

<?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
}
?>

but if i run the file, it returns (Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /srv/disk11/1165502/www/tst.scienceontheweb.net/admin/manager/result-weekly.php on line 44 )

What am I doing wrong? Please someone help me.

  • 写回答

1条回答 默认 最新

  • douyue8364 2016-08-10 15:56
    关注

    mysql_error returns false, because thereis an error in you query. You should correct it to :

    $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 . '"');
    

    Otherwise, your query is :

    SELECT sum(mball202) FROM manager WHERE dateid >= date('Y-m-d', 1470693600) AND dateid <= date('Y-m-d', 1470780000)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。
  • ¥15 GitHubssh虚拟机连接不上