代码之家  ›  专栏  ›  技术社区  ›  Will Hay

如何从数据库中提取记录,调整它们并将其插入另一列?

  •  0
  • Will Hay  · 技术社区  · 9 年前

    $conn = update_dao_connect();
    $get_stmt = $conn->prepare("select id, name from story");
    $update_stmt = $conn->prepare("update story set permalink=? where id=?");
    $update_stmt->bind_param("si", $permalink, $id);
    if($get_stmt->execute()){
        $get_stmt->bind_result($id, $name);
        while($get_stmt->fetch()){
            $permalink = generate_story_permalink($name);
            if(!$update_stmt->execute()){
                error_log("update permalink failed for kamp id: ".$id.", name: ".$name.", permalink: ".$permalink);
            }
        }
    }
    else {
        error_log("execute get all story for copy_story_names_to_permalinks fail");
    }
    

    终端输出(用于数据库中的所有记录):
    为故事id:198更新permalink失败,名称:Funding Circle的故事,permalink:Funding circles的故事
    更新permalink失败,故事id:199,名称:Rentah,permalink:Rentah-1

    1 回复  |  直到 9 年前
        1
  •  0
  •   Will Hay    9 年前

    解决方案是添加$get\u stmt->store_result();$get\u stmt之后->执行();就像这个答案一样 Commands out of sync; you can't run this command now