Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6335737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:54:52+00:00 2026-05-24T18:54:52+00:00

I have the following PHP code that allows me to read in from a

  • 0

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

?>

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:54:54+00:00Added an answer on May 24, 2026 at 6:54 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a line of PHP code that does the following: $xml = <xml><request>...[snipped
In PHP I have the following code that gets a file submitted through CGI:
I have code that looks like the following: <form id=MyForm name=MyForm method=post action=index.php> <input
I have the following PHP code which works out the possible combinations from a
I have the following code: if (include_once(dirname(__FILE__).'/file.php') || include_once(dirname(__FILE__).'/local/file.php') ) { This causes an
I have a global.php file that I call from the top of every script.
I have the following quite simple test PHP code that extracts the data and
I have the following simple Javascript code that allows only digits to be entered
I have the following jquery code that allows me to use tabs, to tab
I have the following code that will check to see if the div text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.