开发者

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. 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) v开发者_开发百科alues ('" . 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

which you can invoke through PHP but this would be a lot better unless you have specific reasons it cannot be used.


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

?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜