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

发布于 2024-11-29 05:21:28 字数 917 浏览 0 评论 0原文

我有以下 PHP 代码,它允许我从文本文件中逐行读取并写入 MySQL 数据库。该文本文件由 13 个值组成,每行用空格分隔。我已经在 SQL 中单独创建了该表(包含所需的所有相关字段:Time、WIMU_ID、Ax、Ay 等),然后运行下面的 PHP 将值插入到表中。我的问题是:有没有办法在下面的 PHP 代码中创建表,而不必先在 SQL 中创建表?任何帮助或建议表示赞赏。 休。

<?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 have the following PHP code that allows me to read in from a text file line by line and write to a MySQL database. The text file consists of 13 values seperated by a space on each line. 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. 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? 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());
            }
    }

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼藏柔 2024-12-06 05:21:28

我认为你真正想要的是这样的:

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

您可以通过 PHP 调用它,但这会好得多,除非您有特定原因无法使用它。

I think what you really want is this:

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.

鸩远一方 2024-12-06 05:21:28

当然,您可以执行如下命令:

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

要获取表创建查询,请在 SQL 中创建表后发出此命令:

SHOW CREATE TABLE `test1`;

编辑:

请注意,您不必在直接使用 SQL,但既然您已经有了,那么发出上述命令将为您提供 PHP 中所需包含的内容,以便在表不存在的情况下在将来的安装中创建表。

另请注意,正如 @Jeremy 所建议的,您不必“滚动自己的”代码来将数据文件导入到表中。创建表后,您可以使用LOAD DATA INFILE... 来填充表。

编辑 2:

以下是基于您的评论的示例:

<?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);

?>

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:

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.

Note also that as @Jeremy suggests, you don't have to "roll your own" code to import a data file to a table. Once you've created the table, you can use LOAD DATA INFILE... to populate the table.

Edit 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);

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文