简体   繁体   English

使用 PHP 从文本文件读取,添加到 MySQL DB 问题

[英]Reading from a text file using PHP, adding to MySQL DB Problem

I have the following PHP code that allows me to read in from a text file line by line and write to a MySQL database.我有以下 PHP 代码,它允许我从文本文件中逐行读取并写入 MySQL 数据库。 The text file consists of 13 values seperated by a space on each line.文本文件由每行用空格分隔的 13 个值组成。 I have created the table separately in SQL, (with all the relevant Fields needed: Time, WIMU_ID, Ax, Ay etc) and then run the PHP below to INSERT the values into the table.我在 SQL 中分别创建了表格(需要所有相关字段:时间、WIMU_ID、Ax、Ay 等),然后运行下面的 PHP 将值插入到表格中。 My question is: Is there any way to create the table within the PHP code below and not have to CREATE the table in SQL first?我的问题是:有没有办法在下面的 PHP 代码中创建表,而不必先在 SQL 中创建表? Any help or suggestions appreciated.任何帮助或建议表示赞赏。 Hugh.休。

<?php

    $connection  = mysql_connect('localhost','root','bonzo123');
    mysql_query('create database gameDB');
    mysql_select_db('gameDB');

    $wx = array_map('trim',file("Thu-Apr-01-09_41_01-2010.txt_calibrated_120Hz.txt"));
    $newwx = array();
    foreach($wx as $i => $line)
    {
            if ($i > 1)
            {
                    $tmp = array_filter(explode(' ',$line));
                    $q = "insert into test1 (Time, WIMU_ID, Ax, Ay, Az, Gx, Gy, Gz, Mx, My, Mz, Ax70, Ay37) values ('" . implode("','",$tmp) . "')";
                    $rw = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
            }
    }

?> ?>

I think what you really want is this:我认为您真正想要的是:

http://dev.mysql.com/doc/refman/5.1/en/load-data.html http://dev.mysql.com/doc/refman/5.1/en/load-data.html

which you can invoke through PHP but this would be a lot better unless you have specific reasons it cannot be used.您可以通过 PHP 调用它,但这会好很多,除非您有特定原因无法使用它。

Sure, you can execute a command like:当然,您可以执行如下命令:

CREATE TABLE IF NOT EXISTS `test1`  (
...
);

To get your table creation query, issue this command after you've already created it in SQL:要获取表创建查询,请在 SQL 中创建它之后发出此命令:

SHOW CREATE TABLE `test1`;

Edit:编辑:

Note that you don't have to create the table in SQL directly, but since you already have, issuing the above command will give you exactly what you need to include in PHP to have it create the table on a future installation should the table not exist.请注意,您不必直接在 SQL 中创建表,但由于您已经拥有,发出上述命令将为您提供您需要在 PHP 中包含的内容,以便在以后的安装中创建表存在。

Note also that as @Jeremy suggests, you don't have to "roll your own" code to import a data file to a table.另请注意,正如@Jeremy 建议的那样,您不必“滚动自己的”代码即可将数据文件导入表。 Once you've created the table, you can use LOAD DATA INFILE... to populate the table.创建表后,您可以使用LOAD DATA INFILE...来填充表。

Edit 2:编辑2:

Here is an example based on your comment:这是基于您的评论的示例:

<?php
// Connect to the database server
$connection = mysql_connect('localhost', 'root', 'bonzo123');
if (!$connection)
    die('Could not connect: ' . mysql_error());

// Create database gameDB
if (mysql_query('create database gameDB', $connection)) {
    echo "Database gameDB created successfully.\n";
} else {
    echo 'Error creating database: ' . mysql_error() . "\n:";
}

// select which database to use
mysql_select_db('gameDB', $connection);

// create table test1 if it doesn't already exist
mysql_query('CREATE TABLE IF NOT EXISTS test1 (
    v1 varchar(100),
    v2 varchar(100),
    v3 varchar(100),
    v4 varchar(100),
    v5 varchar(100),
    v6 varchar(100),
    v7 varchar(100),
    v8 varchar(100),
    v9 varchar(100),
    v10 varchar(100)', $connection);

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM