使用 PHP 从文本文件读取,添加到 MySQL DB 问题
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你真正想要的是这样的:
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.
当然,您可以执行如下命令:
要获取表创建查询,请在 SQL 中创建表后发出此命令:
编辑:
请注意,您不必在直接使用 SQL,但既然您已经有了,那么发出上述命令将为您提供 PHP 中所需包含的内容,以便在表不存在的情况下在将来的安装中创建表。
另请注意,正如 @Jeremy 所建议的,您不必“滚动自己的”代码来将数据文件导入到表中。创建表后,您可以使用
LOAD DATA INFILE...
来填充表。编辑 2:
以下是基于您的评论的示例:
Sure, you can execute a command like:
To get your table creation query, issue this command after you've already created it in SQL:
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: