p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: PHP/MySQL Question


Message #1 by WH Micro <wh_micro@y...> on Thu, 11 Apr 2002 09:43:14 -0700 (PDT)
Hello:

I am in the process of creating a website with PHP and
MySQL. I already created the database and tables. This
data in the tables will be changing regularly, every
week to be exact. There are going to be over 500
records. This database will keep the statistics for
players in a baseball league. I don't feel like
spending hours updating them.

I would like to know what would be the easiest way of
updating these records without having to type each one
of them into insert statements.

I was thinking of creating a script that will
automatically insert and/or update each record but, I
believe I still have to type each one of them
manually. I also thought of creating a CVS file to use
it with the LOAD DATA command.

Anyone has any idea on how I can come with a solution
that won't keep me away from working on other
projects.

Thanks in advance for all your help.

If you need more info email me directly.

Thanks.



=====
www.whmicro.com

__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/
Message #2 by "Dan Ostrowski" <dan@t...> on Thu, 11 Apr 2002 18:09:37
First off, how are these records going to be recorded in the first place? 
In other words, what program/notation are you using to put down these 
values initially?

It's hard for me to offer any advice if I don't know.  Are you, say, using 
a home-made C program to type information into your laptop?  That kind of 
thing.

The easiest way is, obviously, to initially put the stats into a file that 
PHP can read.  Then it's a simple task of running a script that checks 
every line in the file, and updates/inserts as nessesary.

I.E. - If you record the numbers into a text file like thus:

First, Last, Number, PtsScored, Assists, etc.

Then you can get PHP to parse the file, line by line, and either break it 
into an array (using split) or use regexps to check the name or somesuch.

I would split it into an array, then grab the first and last name.  Then 
you query your database really quick and see if the name is there. If it 
IS, then you run an update query. If it isn't then you do an insert query.

That kind of thing.

Again, You would only need to load the file, and then initialize the PHP 
script.

Sorry if this is no help, I just don't know how you are recording the data 
in the first place...


Good luck,
Dan
Message #3 by WH Micro <wh_micro@y...> on Thu, 11 Apr 2002 10:20:17 -0700 (PDT)
Thanks for your reply.

I still don't have any data and don't know how I will
record the data. From the replies to this post I will
be taking some ideas on what would be the easiest and
quickest way to get the data and input it into the
database. 

- Wilson


--- Dan Ostrowski <dan@t...> wrote:
> First off, how are these records going to be
> recorded in the first place? 
> In other words, what program/notation are you using
> to put down these 
> values initially?
> 
> It's hard for me to offer any advice if I don't
> know.  Are you, say, using 
> a home-made C program to type information into your
> laptop?  That kind of 
> thing.
> 
> The easiest way is, obviously, to initially put the
> stats into a file that 
> PHP can read.  Then it's a simple task of running a
> script that checks 
> every line in the file, and updates/inserts as
> nessesary.
> 
> I.E. - If you record the numbers into a text file
> like thus:
> 
> First, Last, Number, PtsScored, Assists, etc.
> 
> Then you can get PHP to parse the file, line by
> line, and either break it 
> into an array (using split) or use regexps to check
> the name or somesuch.
> 
> I would split it into an array, then grab the first
> and last name.  Then 
> you query your database really quick and see if the
> name is there. If it 
> IS, then you run an update query. If it isn't then
> you do an insert query.
> 
> That kind of thing.
> 
> Again, You would only need to load the file, and
> then initialize the PHP 
> script.
> 
> Sorry if this is no help, I just don't know how you
> are recording the data 
> in the first place...
> 
> 
> Good luck,
> Dan


=====
www.whmicro.com

__________________________________________________
Do You Yahoo!?
Yahoo! Tax Center - online filing with TurboTax
http://taxes.yahoo.com/
Message #4 by "Nikolai Devereaux" <yomama@u...> on Thu, 11 Apr 2002 11:00:04 -0700
Well, somehow or another the information has to be typed.  If you type it
all into comma-separated values file, for example, then you can do something
like this:

<?php

//db connection up here.

$data = file("your file name here");
for($i = 0; $data[$i]; ++$i)
{
   list($name, $number, $hits, $rbis, ...) = each explode(',', $data[$i]));

   $query = "UPDATE stats SET hits='$hits', rbis='$rbis', ..."
          . "WHERE name='$name' AND number='$number'";

   db_query($query);
}
?>

DISCLAIMER:
As always, this code is no where near complete, and is only posted to
suggest a possible method of attack.

=)

nik

Message #5 by "Dan Ostrowski" <dan@t...> on Thu, 11 Apr 2002 19:47:20
Right.

Just the kind of thing I was thinking of... for typing the data in, if you 
are using, as I am assuming, a laptop to go to the games and record them, 
a simple Perl or C program could be used to make the text files.

If you are recording the entries at HOME, well you might as well just 
create one PHP script to do all the recording and just use form entries!  
Cut out the middle man.

> 
Well, somehow or another the information has to be typed.  If you type it
all into comma-separated values file, for example, then you can do 
something
like this:

<?php

//db connection up here.

$data = file("your file name here");
for($i = 0; $data[$i]; ++$i)
{
   list($name, $number, $hits, $rbis, ...) = each explode(',', $data[$i]));

   $query = "UPDATE stats SET hits='$hits', rbis='$rbis', ..."
          . "WHERE name='$name' AND number='$number'";

   db_query($query);
}
?>

DISCLAIMER:
As always, this code is no where near complete, and is only posted to
suggest a possible method of attack.

=)

nik


  Return to Index