mysql取出數據並使用PHP將其組裝成insert語句

文章地址:https://blog.lmlyz.online/index/detail/id/85.html

	$config_link = @mysqli_connect("127.0.0.1", "root", "root", "db_name", "3306");
    if (!$config_link) {
        die('Could not connect: '. mysqli_error() ."\n");
    }
    @mysqli_query($config_link,"set names utf8");

    $_tmpRes = @mysqli_query($config_link,"select * from `table_name`");

    if(!$_tmpRes) die("SQL(fetchRow):{$sql} ".mysqli_error($config_link));
    while ($res = mysqli_fetch_assoc($_tmpRes)) {
        $result[] = $res;
    }
    
    $tmp_value = "";
    $tmp_key = "";
    if (!empty($result)) {
        foreach ($result as $key => $value) {
            $tmp_key = '(`'.implode('`,`', array_keys($value)).'`) values ';
            $tmp_value .= '("'.implode('","', array_values($value)).'"),';
        }
        $sql = "insert into `table_name`".$tmp_key.$tmp_value;
        $sql = substr($sql, 0, -1).';';
    }

    print_r($sql);exit();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章