Parsing Tab Delimited file

Dear list -

Thanks for all your help.

I have a tab delimited file which I wish to import into a data
base. In MySQL I can use the LOAD DATA command. As far as I know,
that command cannot be used in PHP as a mysqli_query. As I
understand, I have to parse the file and use the INSERT command.

How do I do it? Code samples, please.

Thanks.

Ethan



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ethan Rosenberg [ So, 24 Oktober 2010 20:46 ] [ ID #2049529 ]

Re: Parsing Tab Delimited file

On Oct 24, 2010, at 2:46 PM, Ethan Rosenberg wrote:

> Dear list -
>
> Thanks for all your help.
>
> I have a tab delimited file which I wish to import into a data
> base. In MySQL I can use the LOAD DATA command. As far as I know,
> that command cannot be used in PHP as a mysqli_query. As I
> understand, I have to parse the file and use the INSERT command.
>
> How do I do it? Code samples, please.
>
> Thanks.

http://tinyurl.com/2fnm48f



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jason Pruim [ So, 24 Oktober 2010 20:54 ] [ ID #2049530 ]

Re: Parsing Tab Delimited file

Hi Ethan,

fist you need to read the file, you could use the file() function
which reads the file as an array, being each line an element;
the you have to navigate the array and exploding the lines into an array:

$data =3D file('myfile.txt');

foreach ($data AS $row) {
$row =3D explode("\n", trim($row));
var_dump($row);
}

And there you go.

Cheers.

On 24 October 2010 16:46, Ethan Rosenberg <ethros [at] earthlink.net> wrote:
> Dear list -
>
> Thanks for all your help.
>
> I have a tab delimited file which I wish to import into a data base. =C2=
=A0In
> MySQL I can use the LOAD DATA command. =C2=A0As far as I know, that comma=
nd
> cannot be used in PHP as a mysqli_query. =C2=A0As I understand, I have to=
parse
> the file and use the INSERT command.
>
> How do I do it? =C2=A0Code samples, please.
>
> Thanks.
>
> Ethan
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--
Andr=C3=A9s G. Monta=C3=B1ez
Zend Certified Engineer
Montevideo - Uruguay

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
andresmontanez [ So, 24 Oktober 2010 20:56 ] [ ID #2049531 ]

Re: Parsing Tab Delimited file

> Dear list -
>
> Thanks for all your help.
>
> I have a tab delimited file which I wish to import into a data
> base. In MySQL I can use the LOAD DATA command. As far as I know,
> that command cannot be used in PHP as a mysqli_query. As I
> understand, I have to parse the file and use the INSERT command.

It can be done, IF the MySQL server allows you to do it.. Check the
servers configuration.

> How do I do it? Code samples, please.
>
> Thanks.
>
> Ethan
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

--
Niel Archer
niel.archer (at) blueyonder.co.uk



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Niel Archer [ So, 24 Oktober 2010 21:17 ] [ ID #2049532 ]

Re: Parsing Tab Delimited file

At 02:54 PM 10/24/2010, Jason Pruim wrote:

>On Oct 24, 2010, at 2:46 PM, Ethan Rosenberg wrote:
>
>>Dear list -
>>
>>Thanks for all your help.
>>
>>I have a tab delimited file which I wish to import into a data
>>base. In MySQL I can use the LOAD DATA command. As far as I know,
>>that command cannot be used in PHP as a mysqli_query. As I
>>understand, I have to parse the file and use the INSERT command.
>>
>>How do I do it? Code samples, please.
>>
>>Thanks.
>
>http://tinyurl.com/2fnm48f
>
>
++++++++++
Jayson -

Sorry for the delay.

Thanks. With what you and Andres send, it works beautifully.

Ethan



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ethan Rosenberg [ Di, 26 Oktober 2010 15:51 ] [ ID #2049612 ]

Re: Parsing Tab Delimited file

At 02:56 PM 10/24/2010, Andr=C3=A9s G. Monta=C3=B1ez wrote:
>Hi Ethan,
>
>fist you need to read the file, you could use the file() function
>which reads the file as an array, being each line an element;
>the you have to navigate the array and exploding the lines into an array:
>
>$data =3D file('myfile.txt');
>
>foreach ($data AS $row) {
> $row =3D explode("\n", trim($row));
> var_dump($row);
>}
>
>And there you go.
>
>Cheers.
>
>On 24 October 2010 16:46, Ethan Rosenberg <ethros [at] earthlink.net> wrote:
> > Dear list -
> >
> > Thanks for all your help.
> >
> > I have a tab delimited file which I wish to import into a data base. =C2=
In
> > MySQL I can use the LOAD DATA command. =C2 As far as I know, that=
command
> > cannot be used in PHP as a mysqli_query. =C2 As I understand, I have to=
parse
> > the file and use the INSERT command.
> >
> > How do I do it? =C2 Code samples, please.
> >
> > Thanks.
> >
> > Ethan
> >
> >
> >
> > --
> > PHP Database Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>
>
>--
>Andr=C3=A9s G. Monta=C3=B1ez
>Zend Certified Engineer
>Montevideo - Uruguay
++++++++
Andres -

Sorry for the delay.

Thanks. With what you and Jayson sent, it works beautifully.

Ethan



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ethan Rosenberg [ Di, 26 Oktober 2010 15:52 ] [ ID #2049613 ]

Re: Parsing Tab Delimited file

On 26 October 2010 14:52, Ethan Rosenberg <ethros [at] earthlink.net> wrote:
> At 02:56 PM 10/24/2010, Andr=C3=83=C2=A9s G. Monta=C3=83=C2=B1ez wrote:
>>
>> Hi Ethan,
>>
>> fist you need to read the file, you could use the file() function
>> which reads the file as an array, being each line an element;
>> the you have to navigate the array and exploding the lines into an array=
:
>>
>> $data =3D file('myfile.txt');
>>
>> foreach ($data AS $row) {
>> =C2=A0$row =3D explode("\n", trim($row));
>> =C2=A0var_dump($row);
>> }
>>
>> And there you go.
>>
>> Cheers.
>>
>> On 24 October 2010 16:46, Ethan Rosenberg <ethros [at] earthlink.net> wrote:
>> > Dear list -
>> >
>> > Thanks for all your help.
>> >
>> > I have a tab delimited file which I wish to import into a data base. =
=C3=82
>> > In
>> > MySQL I can use the LOAD DATA command. =C3=82 As far as I know, that c=
ommand
>> > cannot be used in PHP as a mysqli_query. =C3=82 As I understand, I hav=
e to
>> > parse
>> > the file and use the INSERT command.
>> >
>> > How do I do it? =C3=82 Code samples, please.
>> >
>> > Thanks.
>> >
>> > Ethan
>> >
>> >
>> >
>> > --
>> > PHP Database Mailing List (http://www.php.net/)
>> > To unsubscribe, visit: http://www.php.net/unsub.php
>> >
>> >
>>
>>
>>
>> --
>> Andr=C3=83=C2=A9s G. Monta=C3=83=C2=B1ez
>> Zend Certified Engineer
>> Montevideo - Uruguay
>
> ++++++++
> Andres -
>
> Sorry for the delay.
>
> Thanks. =C2=A0With what you and Jayson sent, =C2=A0it works beautifully.
>
> Ethan
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

If you need to break the tab file apart then fgetcsv() is probably
going to help.

Just use "\t" for the $delimiter.

Pretty much.

But that's assuming you aren't using a mysql tool to import the data
directly. (I do a BULK INSERT in MS SQL).

--
Richard Quadling
Twitter : EE : Zend
[at] RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Richard Quadling [ Di, 26 Oktober 2010 16:14 ] [ ID #2049614 ]
PHP » gmane.comp.php.database » Parsing Tab Delimited file

Vorheriges Thema: Books
Nächstes Thema: Creating an INDEX on multiple tables?