php - mysql 和 php - 使用已经转换为 int 的商更新 int 列给我留下分子或零

标签 php mysql codeigniter activerecord

请查看下面的更新!

这就是我想要做的:获取一篇文章的最后一次更新时间戳(存储为int),连同最后一次更新时的流行度(也是int),计算新的流行度和由此产生的趋势( = 自上次更新以来每个时间单位流行度变化的估计)并更新文章。

问题是,写入数据库行的值几乎总是趋势方程式的分子(因此计算出的流行度)或 0。流行度的静态值不会改变任何事情,但是静态值对于 last_update 或 time() 导致插入正确的值。还有一些奇怪的事情:将 last_update 排除在更新查询之外也会导致插入正确的趋势值。这就像 last_update 列影响趋势列,我不明白如何。

这是我的代码,请注意我使用的是 codeigniter,所以我使用 activerecord 来获取文章。另外,我对几乎所有数值都使用了 intval(),因为我的第一个猜测是数据类型问题:

$this->db->where('id', 1);
$article_query = $this->db->get('articles');
$article = $article_query->row_array();

$article['last_update'] = intval($article['last_update']);
$time = intval(time());
$new_popularity = // Calculate Popularity here (I tried with static values, doesn't affect the result) ;
$time_diff = intval(($time - $article['last_update']));

$trend = intval((($new_popularity - $article['popularity']) / $time_diff)); 

var_dump($trend);

$this->db->query('UPDATE `articles` SET `popularity` = '.$new_popularity.',  `trend` = '.$trend.' WHERE `id` =  1 ');

var dump 为我提供了一个具有预期值的 int。查询日志还显示了预期的查询,类似于

UPDATE `articles` SET `popularity` = 50000, `last_update` = 1374840645, `trend` = 10 WHERE `id` =  1

但是插入了 50000 个趋势。如前所述,$time 和 $article['last_update'] 的静态值可以解决问题,将 last_update 排除在查询之外也是如此。我还对每个值尝试了 ceil()、floor()、cast (int),但没有任何效果。

所有涉及的列都是int(11),应该够大了。我还尝试了 varchar 的趋势并将其作为字符串插入 - 没有成功。我已经处理这个问题 2 天了,我真的很想得到任何帮助!

我在 Windows 7 上运行 MySQL 5.5.32。

请让怪异消失,谢谢!

编辑: 进一步澄清:出于测试目的,我将 $article['popularity'] 设置为 0,因此我可以重复测试并仍然得到 >0 的趋势。这就是插入 50000 而不是实际差异的原因。

更新:这是我的代码所在的位置。使用 ON UPDATE CURRENT_TIMESTAMP 将 last_update 设置为 TIMEZONE 类型。

$this->db->where('id', 1);
$article_query = $this->db->get('articles');
$article = $article_query->row_array();

date_default_timezone_set('Europe/Berlin');
$article['last_update'] = intval(strtotime($article['last_update']));

$time = intval(time());
$new_popularity = 50000; // Static test value
$time_diff = intval(($time - $article['last_update']));

$trend = intval((($new_popularity - 0) / $time_diff)); // Zero for testing purposes only, so that there will always be a positive trend.

var_dump($trend);

$this->db->_protect_identifiers=false;
$this->db->query('UPDATE articles SET popularity = ?,  trend = ? WHERE id =  ?', Array($new_popularity, $trend, 1));

查询日志:

UPDATE articles SET popularity = 50000, trend = 403 WHERE id =  1

通过 phpMyAdmin 的实际值:流行度 = 50000,趋势 = 50000。我现在还在干净安装的 apache 和 mysql 上运行代码,使用 php 5.4.15,mysql 5.6.11。接下来我会尝试完全不使用 codeigniter 我猜...

更新:我自己的错误,我没有仔细阅读日志并且没有注意到发生这样的事情:

130730 17:25:48    51 Connect   root@localhost on zeenr
           51 Init DB   zeenr
           51 Query SET NAMES utf8
           51 Query SET SESSION sql_mode="STRICT_ALL_TABLES"
           51 Query SELECT *
FROM (`articles`)
WHERE `id` =  1
           51 Query UPDATE articles SET `popularity` = 50000, `trend` = 179 WHERE `id` =  1
           51 Quit  
130730 17:25:49    52 Connect   root@localhost on zeenr
           52 Init DB   zeenr
           52 Query SET NAMES utf8
           52 Query SET SESSION sql_mode="STRICT_ALL_TABLES"
           52 Query SELECT *
FROM (`articles`)
WHERE `id` =  1
           52 Query UPDATE articles SET `popularity` = 50000, `trend` = 50000 WHERE `id` =  1
           52 Quit  

为什么会这样?我的代码中没有任何循环。

最佳答案

要真正知道运行了什么查询,您应该启用 MySql 日志:

SET GLOBAL general_log = 'ON';

运行查询后,C:\xampp\mysql\data 中有一个日志文件。 (以我为例)

也可以尝试不保护标识符并使​​用查询绑定(bind):

$this->db->query(
    'UPDATE articles SET popularity = ?, trend = ? WHERE id = ?',
    array($new_popularity, $trend, 1)
);

编辑:

您尝试过 active record 吗?语法?

$data = array
(
    'popularity' => $new_popularity,
    'trend' => $trend,
);

$this->db->where('id', 1);
$this->db->update('articles', $data);

关于php - mysql 和 php - 使用已经转换为 int 的商更新 int 列给我留下分子或零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17881087/

相关文章:

php - 以编程方式创建 WooCommerce 订单时设置外部订单 ID

php - 如何将Post数组存储到mysql中

java - 由于基础异常 : Connection Time Out 导致通信链接失败

codeigniter - 如何在代码点火器中编写连接选择语句

php - 在 codeigniter 中使用 required once 或在 helper 中创建一个函数哪个是安全的?

PHP 实时通知(推送器?)

php - 在提交到数据库之前返回已获取的字段

php - 如何在我的表中获取最后一个 reasonID?

php - MySQL order by 哪里大于按字母顺序排列的短语?

mysql - 如何根据另一个表中的值删除一个表中的记录?