gpt4 book ai didi

php - 使用php将大量数据插入mysql

转载 作者:行者123 更新时间:2023-11-29 09:01:40 25 4
gpt4 key购买 nike

我想生成随机字符串并插入数据库,插入应该高效且稳健。我有一个生成字符串的函数,如下所示:

public function getRandString(){
$string= //generats a random string
return $string;
}

数据库连接:

$con = mysql_connect($host, $user, $pass)
or die ( " Not able to connect to server ");
mysql_select_db($db_name);

现在我想创建一个方法,将随机字符串一次插入数据库 100 万行,每行包含 2 列随机字符串,我应该如何处理?

感谢您提前提供帮助。

最佳答案

两种方法:

  1. 使用php创建sql文件通过命令行、phpmyadmin、sequelpro、tomcat等导入

    • 打开文件
    • 附加INSERT INTO任何值('randomshit','randomshit');任意次数
    • 关闭文件,用它来填充数据库
  2. 使用您上面提到的连接:

    • 批量创建 INSERT INTOwhatever VALUES( 'randomshit', 'randomshit' ); 25000

      $howMany = 40;
      // do this 40 times (40x25000 ) = 1,000,000
      while( --$howmany ) {
      $random = "";
      $randomMany = 25000;
      while( --$randomMany ) {
      $random += sprintf( "INSERT INTO whatever VALUES ('%s', '%s'); ", rand_text(), rand_text() );
      }
      // you'll now have a big list (25000 ) of insert queries to insert
      mysql_query( $random );
      }

关于php - 使用php将大量数据插入mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8391656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com