【问题标题】:MySQLi prepare update not updating the databaseMySQLi准备更新不更新数据库
【发布时间】:2015-05-13 04:59:59
【问题描述】:

正如标题所述,我的 MySQLi 准备更新实际上并没有更新数据库。我检查了 MySQL 日志 - 没有错误。这是有问题的代码:

public function update_tweet($tweet)
{
    $prepared_update = $this->connection->prepare("UPDATE Tweets 
                     SET `text` = ?, `algo_score` = ?, `has_algo_score` = ?, `baseline_score` = ?, `has_baseline_score` = ?, `is_sanitized` = ?
                     WHERE `twitter_id` = ?");
    mysqli_stmt_bind_param($prepared_update, "sssssss", $tweet['text'], $tweet['algo_score'], $tweet['has_algo_score'], $tweet['baseline_score'], $tweet['has_baseline_score'], $tweet['is_sanitized'], $tweet['tweet_id']);
    mysqli_execute($prepared_update) or die(mysqli_error($this->connection));
    $prepared_update->close();
}

以及传入的推文数组示例:

Array ( [id] => 2 
        [twitter_id] => 595463376026734592 
        [text] => History has a way of repeating itself 
        [algo_score] => 0 
        [has_algo_score] => 1 
        [baseline_score] => 0 
        [has_baseline_score] => 1 
        [is_sanitized] => 1 
)

还有表架构:

没有 PHP 错误或 MySQL 错误。我做错了什么?

【问题讨论】:

  • 检查 mySQL 查询日志以查看是否有任何实际通过?我的地址是/usr/local/mysql/data/mysql.log
  • 感谢您的评论 - 这只是一个简单的拼写错误。菜鸟状态:)

标签: php mysql mysqli prepared-statement


【解决方案1】:

你的情况:

WHERE `twitter_id` = ?

您要绑定的变量是:

$tweet['tweet_id']

虽然你得到的数组是:

Array ( [id] => 2 
        [twitter_id] => 595463376026734592

所以您使用了错误的索引,并且由于索引 tweet_id 未定义(这只是一个通知,您没有收到任何错误或警告)您的 WHERE 条件永远不会返回 true,因此 - 没有任何操作被带走。

【讨论】:

  • 天哪,真尴尬……谢谢。还有什么方法可以报告通知?我在输出页面上设置了error_reporting(E_ALL);ini_set("display_errors", 1);
  • 不客气,每个人都会时不时遇到这种情况,所以没关系。 E_ALL 应该显示通知。
猜你喜欢
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-30
相关资源
最近更新 更多