php - 在选择查询循环中插入查询

标签 php mysql

我创建了下面的函数,其中包含几个 mysql 查询,这似乎会产生问题。因为当我运行此函数时它返回以下错误:

Errormessage: Commands out of sync; you can't run this command now

我尝试包含 next_result(),但没有任何区别?

function retrievePlayerTweets(){
    global $con;
    $query = $con->prepare("Select players.fullname, players.twitter_user, team.id as teamId FROM players, team WHERE players.teamId = team.id");
    $query->execute();
    $query->bind_result($fullname, $twitter_user, $teamId);

    while ($query->fetch()) {

        foreach(retrieveUserTweets($twitter_user) as $twitterData) {
            $id = $twitterData['id_str'];
            $text = $twitterData['text'];
            $name = $twitterData['user']['name'];
            $dateString = $twitterData['created_at'];
            $favoriteCount = $twitterData['favorite_count'];
            $date = date('Y-m-d H:i:s', strtotime($dateString));

            if ($insert_tweet = $con->prepare("INSERT IGNORE INTO tweets (`fullname`, `username`, `text`, `created`, `teamId`, `twitterId`, `favoriteCount`) VALUES (?, ?, ?, ?, ?, ?, ?)")) {

                $insert_tweet->bind_param("ssssisi", $name, $twitter_user, $text, $date, $teamId, $id, $favoriteCount);
                $insert_tweet->execute();
                $con->next_result();

            } else {
                die("Errormessage: ". $con->error);
            }
        }
    }
}

最佳答案

如果您的命令不同步;您现在无法在客户端代码中运行此命令,您正在以错误的顺序调用客户端函数。

例如,如果您正在使用 mysql_use_result() 并在调用 mysql_free_result() 之前尝试执行新查询,则可能会发生这种情况。如果您尝试执行两个返回数据的查询而不在其间调用 mysql_use_result() 或 mysql_store_result() ,也可能会发生这种情况。

https://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html

关于php - 在选择查询循环中插入查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30528789/

相关文章:

php - 递归概览文件 PHP/Laravel

PHP 复杂的 MySQL 查询从不同的表计数

MySQL 函数给出相同的结果

mysql - 查找部门内员工的平均工资偏差

php - 如果该行中的某个单元格为空,如何隐藏整行?

php - 为什么我不应该在 PHP 中使用 mysql_* 函数?

php - 为什么 if/else vs. or/exit 在 PHP 中?

javascript - 如何从发送电子邮件中删除特定数据

javascript - getElementById 给出结果加上字符串

sql - 动态 sql 与存储过程 - 优缺点?