0

I would like to take information from TXT and insert it in my database.

I want to create a loop that starts to read the first line to the two empty lines, Each line loop is independent parameter.

when the first line/line is "name" paramter And the second row is the street paramter and the third line is a city paramter and the rest of the remaining lines are text parameter.

It is important to note that not all loops equal number of their lines and what separates each loop is 2 blank lines.

<?php

$file = file('try1.txt');  

for ($i = 0; $i < count($file); $i++) {

  $sql = "INSERT INTO `table` (`name`,`street`,`city`,`text`) VALUES ('$name','$street','$city','$text')"; 
        mysql_query($sql);
}
?>

the file: try1.txt

3
  • Why you taged your question with pdo mysqli if you are using mysql functions? Anyway, do not do that, they are deprecated. Commented Dec 17, 2014 at 12:32
  • @lolka_bolka You answered to yourself. I would also use the PDO and MYSQLI Commented Dec 17, 2014 at 12:34
  • Just replace the tabulators inside of it. Commented Dec 17, 2014 at 13:48

2 Answers 2

1

Do you want something like this?

$content = "Name
Street
City
This
is
some
text


Name2
Street2
City2
Something
else
string";

//$content = file_get_contents("try1.txt");
//$link = mysqli_connect("localhost", "dbuser", "dbpass", "dbname");
$string = str_replace("\r", '', $content);
$blocks = explode("\n\n\n", $string);
foreach ($blocks as $block) {
    $lines = explode("\n", $block);
    $name = $city = $street = $text = "";
    if (!empty($lines[0])) {
        $name = $lines[0];
    }
    if (!empty($lines[1])) {
        $street = $lines[1];
    }
    if (!empty($lines[2])) {
        $city = $lines[2];
    }
    if (!empty($lines[3])) {
        for ($i=3; $i < count($lines); $i++) {
            $text .= " " . $lines[$i];
        }
        $text = ltrim($text," ");
    }
    var_dump ($name);
    var_dump ($street);
    var_dump ($city);
    var_dump ($text);
    echo '----------------------------'."<br />";
    /*
    $sql = "INSERT INTO `table` (`name`, `street`, `city`, `text`)"
        . " VALUES ('".mysqli_real_escape_string($link, $name)."',"
        . "'".mysqli_real_escape_string($link, $street)."',"
        . "'".mysqli_real_escape_string($link, $city)."',"
        . "'".mysqli_real_escape_string($link, $text)."')";
    mysqli_query($link, $sql);
     * 
     */
}

Output is

string 'Name' (length=4)
string 'Street' (length=6)
string 'City' (length=4)
string 'This is some text' (length=17)
----------------------------
string 'Name2' (length=5)
string 'Street2' (length=7)
string 'City2' (length=5)
string 'Something else string' (length=21)
----------------------------

NOTE:
So what you need to do:

  • Remove the $content and uncomment that row, where file_get_contents is.

  • Uncomment the $link row, and set your connection parameters.

  • Remove var_dumps, and echo "-----" lines.

  • Uncomment the rest.

In this example, I've used the mysqli as procedural style, check the document for PDO or mysqli how to use them OOP style.

Sign up to request clarification or add additional context in comments.

5 Comments

It does not work well for me text parameter.i think its because UTF-8 (I translated from Hebrew to English text) the real text is in hebrew. ->I added an active link to a txt file
That is nothing about UTF-8. The problem is, you have no 2 new lines. Open your file, and see, there have a tab character in every 2 new lines in 1st line.
When I translate the file into English it works.That's why I said it might have a link to UTF-8. What can I do with the original txt file?
Replace the tab characters in your file to nothing.
tnx a lot you are the best
0

Create A script that reads the file and stores it into array with key, have a database with e corresponding columns, create a query "INSERT INTO your_table VALUES ($data['value1'], $data['value2'])";

1 Comment

I do not know how. Show me Please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.