mysql取出数据并使用PHP将其组装成insert语句

   2020-07-03 10:15:01

mysql php

        $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();

相关评论:

靡不有初|  当前时间:  |  网站运行时间:  |鲜克有终

今年剩余【农历】:

粤ICP备19080315号