0

我试图

  • table使用我从中获得的字段创建一个fgetcsv()
  • table-name字段将是日期(如13/10/2012)。我想将它附加到另一个字符串并将其用作我的表名。

到目前为止我的代码:

if (($handle = fopen($this->filetemp, "r")) !== FALSE) {

    $ctr = 1;
    $tablename ="";

    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if($ctr ==1) {

            $tablename =  $data[19];

            $query = "CREATE TABLE  ". $tablename ."( PlayerName VARCHAR(60),Handycap INT UNSIGNED NOT NULL,Score INT NOT NULL,Front DOUBLE NOT NULL,Back DOUBLE,Prize VARCHAR(80));" ;
            mysqli_query($this->dbconn,$query);

            $name = str_replace("'"," ",$data[9]);
            mysqli_query($this->dbconn,"INSERT INTO  ". $tablename . " (PlayerName,Handycap,Score,Front,Back) VALUES ('$name', '$data[10]', '$data[11]','$data[12]','$data[13]')");

            $ctr++;
        }
        else if ($ctr > 1)  {
            $name = str_replace("'"," ",$data[9]);
            mysqli_query($this->dbconn,"INSERT INTO  ". $tablename . " (PlayerName,Handycap,Score,Front,Back) VALUES ('$name', '$data[10]', '$data[11]','$data[12]','$data[13]')");

            $ctr++;
        }
    }
    fclose($handle);
}
  • 目前我只是想使用索引 19 处的日期

    但是表没有创建!!当我尝试时。我也尝试使用其他索引值创建表名,但它仍然不起作用。

我认为我以错误的方式完全解决了这个问题,任何帮助都将不胜感激。干杯!

采样 CSV 记录:

"高尔夫俱乐部","比赛结果报告","十月月度奖牌 - 2012 年 10 月 13 日 [Stroke]","球员姓名","H'cap","Score","Front","Back","新盘口","firstname, secondname","9","66","34.5","31.5","","球员打印:",1,"报告由 Genesys Convenor 制作。","打印在: ",13/10/2012,"页码:",1

"高尔夫俱乐部","比赛结果报告","十月月度奖牌 - 2012 年 10 月 13 日 [Stroke]","球员姓名","H'cap","Score","Front","Back","新盘口","firstname, secondname","18","66","33.0","33.0","","Players print:",2,"Report 由 Genesys Convenor 制作。","印刷在: ",13/10/2012,"页码:",1

4

1 回答 1

0

帖子中的 CSV 有所帮助;使用撇号作为表名。

更改以下代码:

$tablename =  $data[19];

到 :

$tablename =  "`".$data[19]."`";

如果您使用以下功能进行调试会更容易var_dump()

PS:请停止使用已弃用的 API。

于 2013-01-29T11:39:26.637 回答