View Single Post
Old 01-16-2005, 10:26 PM   #4 (permalink)
dflowers
Registered User
 
Join Date: Jan 2005
Posts: 4
dflowers is on a distinguished road
Here is what I have done so far

Ok, how do I send this information to a database? I set up the page like this:

<?
$my_array = file("http://www.fpfr.com/calls/call.csv");
?>

<?
foreach($my_array as $line){
$row = explode($line,",");

// maybe you can scan for duplicates here before you insert the line
$result = mysql_query("select count(*) as count from table where field1='".$row[0]."'");
$num = mysql_result($result,0,0);

// now only insert the row into the database table if it's not a duplicate
if($num==0){
$result = mysql_query("insert into table (field1,field2,field3) values('".$row[0]."','".$row[1]."','".$row[2]."')");
}
}
?>

I called the page csvtest10.php I got this error

Warning: mysql_query(): Access denied for user: 'apache@localhost' (Using password: NO) in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 10

Warning: mysql_query(): A link to the server could not be established in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 10

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 11

Warning: mysql_query(): Access denied for user: 'apache@localhost' (Using password: NO) in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 15

Warning: mysql_query(): A link to the server could not be established in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 15

Warning: mysql_query(): Access denied for user: 'apache@localhost' (Using password: NO) in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 10

Warning: mysql_query(): A link to the server could not be established in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 10

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 11

Warning: mysql_query(): Access denied for user: 'apache@localhost' (Using password: NO) in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 15

Warning: mysql_query(): A link to the server could not be established in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 15

Warning: mysql_query(): Access denied for user: 'apache@localhost' (Using password: NO) in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 10

Warning: mysql_query(): A link to the server could not be established in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 10

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 11

Warning: mysql_query(): Access denied for user: 'apache@localhost' (Using password: NO) in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 15

Warning: mysql_query(): A link to the server could not be established in /usr/local/psa/home/vhosts/nhcfirerescue.org/httpdocs/csvtest10.php on line 15

It just repeats over and over. Sorry, but I have very little clue what I am doing here. My understanding of PHP is very basic.

Quote:
Originally Posted by sde
well that is a lot of asking for 1 post, so i'll just show you how to get started for now. later, you can fire off more direct and specific questions as it starts to take shape.

first of all, php has a great function to read external files. file()

this function reads the file you give it into an array. so each line of the file is one element of the array.
PHP Code:
<?
$my_array 
file("http://www.domain.com/folder/file.csv");
?>
now, what you do next is going to be up to you. your pretty limited with arrays for filtering data management, so you might want to use a mysql database to temporarily store the csv information before you add the extra column.

after you read the file into an array, you might do something like this:
PHP Code:
<?
foreach($my_array as $line){
  
$row explode($line,",");

  
// maybe you can scan for duplicates here before you insert the line
  
$result mysql_query("select count(*) as count from table where field1='".$row[0]."'");
  
$num mysql_result($result,0,0);

  
// now only insert the row into the database table if it's not a duplicate
  
if($num==0){
    
$result mysql_query("insert into table (field1,field2,field3) values('".$row[0]."','".$row[1]."','".$row[2]."')");
  }
}
?>
without the data field, it's difficult to make a mock-up. you could either check the csv for updates, or just run the entire process all over again. that may be better since you have to read in the entire csv file anyway.

you would need to set this php script to run on cron or i think windows could run it as a 'scheduled task' (not sure on windows), .. then the php page that is being viewed is just viewing the latest database records.

my response is kind of all over the place, but your post is a little bit too. it's difficult to answer clearly, but i hope this helps a little.
dflowers is offline   Reply With Quote