I have a question about reading a file line by line
i have a txt file which has some data's and i want to design a php file to read this data line by line and insert the datas into mysql in each line
how can i do it with fread or something?
Thanks

    Yes, you can do so with fread(), but in this case [man]file/man might be simpler.

      You might also try using [man]fopen[/man], [man]fgets[/man], and then [man]fclose[/man]. This will get a line and then allow you to do something about it before moving to the next line (handy for handling super-sized files - otherwise [man]file[/man] will work fine for you too).

        so i have a text file like this

        pvp.txt

        account name point age
        account2 name2 point2 age2
        

        theese are some user datas
        for example i want to read and insert it into mysql like this:
        first take account as a variable and insert into mysql then take name as another data in same column with account .......
        then secondly take account2 as a variable and isnert into mysql then take name2 as another data in same column with account 2

        can i do something like this?
        if i can how can i do i looked fgets and file but it was all reading bytes how can i read every line regularly?

          file() read a file line by line, not in a byte stream.
          so

           
          $rows = file(myFile.txt);
          
          foreach ($rows as $line_num => $lines) 
          {
             echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($lines) . "<br />\n";
          }
          
          

          will output each element in the array $rows

            how must i define $line_num and $lines
            or what must i write instead of them
            i want my php file to read every line in that txt file

              that code will read every line in the file...
              file() will take every line in the file and store it in the array $rows
              (so $rows[0] will be the first line, $rows[1] will be the second line and so on)

              foreach($rows as $line_num => $lines) will read each element in the array $rows .... go to php.net to read how 'foreach' works..

                k i understood then one more question

                how can i read variables in one sentece with file or fread command

                So i want to read the text line by line and read variables which is seperated from each other with blanks in one sentence
                is it possible?

                  are you talking about words in a sentence? you wanna split the words right?

                  // NOTE: dont use this code, it'll look messed up, its just so you get the point
                  $lines = file("myfile.txt"); //tha file array
                  foreach($lines as $line)
                  {
                      $words = explode(" ",trim($line)); //make another array full of the words, trim so we dont get confused
                      //ok you can foreach again or do a wee for, i'll do a for
                     for($i=0; $i < count($words); $i++)
                     {
                        print($i . "| Word Is: ".$words[$i]);
                     }
                  }
                  

                  that'll probably not work but thats how to split the sentence into words, which most likely has nothing to do with what your talking about...

                    <?
                    $tname = "textfile_path";
                    $fname = fopen($tname,"rt");

                    while(!feof($fname))
                    {
                    $templine = fgets($fname); // Reads each line from text file
                    // use functions like rewind to move your file pointer if you need to

                           echo "$templine";

                    }

                    fclose($fname);
                    ?>

                    Good Luck
                    TommYNandA

                      how can i count how many lines does the txt file include?
                      because according to this i will make a for loop

                        why use for? when u can use while

                        while(!feof($fname))
                        {

                        }

                        loops until end of file

                        or another way might be ... reading the file using its size as a parameter.. but its not effiecient... file() function takes in whole text file in memory... waste of server resources

                        TommYNandA

                          I did something like this its working good as i wish but feof part is not working so my loop does not stop at the end of the file its looping forever.

                          my txt file is
                          pvp.txt

                          Osman AppSc 60 10 Olympos
                          dakkar pvpci 52 8 freedom
                          

                          and my php file is :

                          <?
                          @mysql_connect("localhost", "Dakkar", "")
                              or die("Veritabani ile baglanti kurulamadi!");
                          @mysql_select_db("uopvp")
                              or die("Veritabaninda bir hata olustu!");
                          
                          $lines = file("pvp.txt");
                          $fname = fopen("pvp.txt","r");
                          
                          for ($i=0;!feof($fname);$i++) {
                          list($account, $name, $puan, $kills, $guild) = explode(" ",$lines[$i]);
                          echo "$account<br>";
                          $sql = "SELECT count(*) FROM pvp WHERE  account='$account' and name='$name' "; 
                          $res = mysql_query($sql) OR die(mysql_error()); 
                          $row = mysql_fetch_row($res);
                             if($row[0] == 0) {
                             @mysql_query("insert into pvp values('','$account','$name','$puan','$kills','$guild','')");
                             echo "<center><b>Your Data Has Been Inserted.</b></center>";
                             }
                             else {
                             mysql_query("UPDATE pvp SET puan='$puan' WHERE account='$account' and name='$name'");
                             mysql_query("UPDATE pvp SET guild='$guild' WHERE account='$account' and name='$name'");
                             mysql_query("UPDATE pvp SET kills='$kills' WHERE account='$account' and name='$name'");
                             echo "<center><b>Your Data Has Been Updated.</b></center>";
                             }
                          }
                          
                          
                          
                          
                          ?>
                          
                          

                          where is the wrong
                          or do i have to put something to end of txt file for impliying the end of file

                            dont do a for loop when using eof,
                            use

                            while( !feof( $fname ) )

                            and since you're using $lines[$i]
                            i would do something like this:

                            $i=0;
                            
                            while( !feof( $fname ) ){
                            ....
                            $lines[$i];
                            ...
                            
                            $i++;
                            }//end of while loop
                             

                            with these modifications it should work!

                              I made my code like this but its still looping forever
                              Do i need to put some special codes to end of the file for php to understand where the file ends.

                              <?
                              @mysql_connect("localhost", "Dakkar", "")
                                  or die("Veritabani ile baglanti kurulamadi!");
                              @mysql_select_db("uopvp")
                                  or die("Veritabaninda bir hata olustu!");
                              
                              $lines = file("pvp.txt");
                              $fname = fopen("pvp.txt","r");
                              
                              $i=0;
                              while( !feof( $fname ) ) {
                              list($account, $name, $puan, $kills, $guild) = explode(" ",$lines[$i]);
                              echo "$account<br>";
                              $sql = "SELECT count(*) FROM pvp WHERE  account='$account' and name='$name' "; 
                              $res = mysql_query($sql) OR die(mysql_error()); 
                              $row = mysql_fetch_row($res);
                                 if($row[0] == 0) {
                                 @mysql_query("insert into pvp values('','$account','$name','$puan','$kills','$guild','')");
                                 echo "<center><b>Veriler Kaydedildi.Tesekkurler.</b></center>";
                                 }
                                 else {
                                 mysql_query("UPDATE pvp SET puan='$puan' WHERE account='$account' and name='$name'");
                                 mysql_query("UPDATE pvp SET guild='$guild' WHERE account='$account' and name='$name'");
                                 mysql_query("UPDATE pvp SET kills='$kills' WHERE account='$account' and name='$name'");
                                 echo "<center><b>Veriler Guncellendi.Tesekkurler.</b></center>";
                                 }
                              $i++;
                              }
                              
                              

                              ?>

                                dont know what to say... wait for someone more experienced and see if he can solve the problem!

                                  Might be some errors bcos made in a hurry:

                                  <?
                                  @mysql_connect("localhost", "Dakkar", "")
                                      or die("Veritabani ile baglanti kurulamadi!");
                                  @mysql_select_db("uopvp")
                                      or die("Veritabaninda bir hata olustu!");
                                  
                                  $file = file("pvp.txt");
                                  
                                  while($lines=each($file)) {
                                          list($account, $name, $puan, $kills, $guild) = explode(" ",$lines);
                                          echo $account.'<br>';
                                          $sql = "SELECT count(*) FROM pvp WHERE  account='$account' and name='$name' ";
                                          $res = mysql_query($sql) OR die(mysql_error());
                                          $row = mysql_fetch_row($res);
                                          if($row[0] == 0) {
                                                  @mysql_query("insert into pvp values('','$account','$name','$puan','$kills','$guild','')");
                                                  echo "<center><b>Veriler Kaydedildi.Tesekkurler.</b></center>";
                                          }
                                          else {
                                                  mysql_query("UPDATE pvp SET puan='$puan',
                                                  guild='$guild',
                                                  kills='$kills' WHERE account='$account' and name='$name'");
                                                  echo "<center><b>Veriler Guncellendi.Tesekkurler.</b></center>";
                                          }
                                  }
                                  ?>

                                    It didnt work and gave this error

                                    Warning: Variable passed to each() is not an array or object in C:\Program Files\Apache Group\Apache2\htdocs\pvp\gecir.php on line 11
                                    
                                      Write a Reply...