Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将数组插入MySQL数据库_Php_Mysql_Arrays - Fatal编程技术网

使用PHP将数组插入MySQL数据库

使用PHP将数组插入MySQL数据库,php,mysql,arrays,Php,Mysql,Arrays,大家好,我是php新手,所以我需要一些帮助 我有一个带有一些信息的.txt,让我们这样说: 970,3584,D53,20170102,OL7GU9,9607,1AT03U2,A, 970,3588,F7A,20170102,OL7GY6,9607,1AT03U2,A, 我想将这些值中的每一个插入到我的数据库中(我已经有了连接) 到目前为止,我的代码是: $myfile = fopen("crawler.txt", "r") or die("Unable to open file!");

大家好,我是php新手,所以我需要一些帮助

我有一个带有一些信息的.txt,让我们这样说:

970,3584,D53,20170102,OL7GU9,9607,1AT03U2,A,
970,3588,F7A,20170102,OL7GY6,9607,1AT03U2,A,
我想将这些值中的每一个插入到我的数据库中(我已经有了连接)

到目前为止,我的代码是:

$myfile = fopen("crawler.txt", "r") or die("Unable to open file!");

    if ($myfile) {
        $columns = explode(",", fread($myfile, filesize("crawler.txt")))
    }
在这一点上,我有一个这样的数组:

array(8825) { [0]=> string(3) "970" [1]=> string(4) "3584" [2]=> string(3) "D53" [3]=> string(8)
"20170102" [4]=> string(6) "OL7GU9" ....and so goes 
如何将数组中的每个元素插入数据库

我当前的代码:

foreach ($columns as $key => $value) { 
    $var1= $columns[0]; 
    $var2= $columns[1]; 
    $var3= $columns[2]; 
    $var4= $columns[3]; 
    $var5= $columns[4]; 
    $var6= $columns[5]; 
    $var7= $columns[6]; 
}

基本上,您只需要构建insert查询并像这样执行它。我在本例中使用了PDO:

$myfile = fopen("crawler.txt", "r") or die("Unable to open file!");

if ($myfile)
{
  while (($line = fgets($myfile)) !== false)
  {
    // remove the comma at the end of every line
    $line = rtrim($line, ',');

    // example with PDO
    $sql = 'INSERT INTO table_name (column_name1, column_name2, ...) VALUES (:param1, :param2, ...)';

    // build the parameter array
    $elements = explode(',', $line);

    // prepare the parameter array
    $parameters = array();
    for($i=0;$i<len($elements); $i++)
    {
      $parameters["param$i"] = $elements[$i];
    }

    $insertquery = $connection->prepare($sql);
    $insertquery->execute($parameters);
  }

  fclose($myfile);
}
$myfile=fopen(“crawler.txt”、“r”)或die(“无法打开文件!”);
如果($myfile)
{
while(($line=fgets($myfile))!==false)
{
//删除每行末尾的逗号
$line=rtrim($line,,');
//PDO示例
$sql='将值(:param1,:param2,…)插入表\u name(列\u name1,列\u name2,…)中;
//构建参数数组
$elements=分解(“,”,$line);
//准备参数数组
$parameters=array();
对于($i=0;$iprepare($sql);
$insertquery->execute($parameters);
}
fclose($myfile);
}

我们遍历文本文件中的每一行。每一行都有一个逗号(,)最后。我们需要删除它,因为explode将返回另一个空数组元素,如果我们不这样做的话。接下来,我们只需构建插入查询并将数组元素传递给它。

您使用的是PDO还是MySqli?您尝试了什么?您有支持这些信息的数据库结构吗?我使用的是MYSQL,我正在尝试这些:foreach($key=>$value的列){$var1=$columns[0];$var2=$columns[1];$var3=$columns[2];$var4=$columns[3];$var5=$columns[4];$var6=$columns[5];$var7=$columns[6];}是的,我有数据库结构来支持这些信息。$handle是什么?哦,很抱歉我打错了。我现在已经更正了!这很有帮助。很高兴听到这个消息。