Training, Open Source
computer languages


PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 

Search our site for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Data becoming corrupted

Posted by Chris_Isaac (Chris Isaac), 16 June 2003
Hi

The following script should load data out of a text file, and put it into a mySQL database.  It does seem to work, but the data on some of the entries gets mixed up, eg, place is set to somerset, with a somerset post code, but the full address says somewhere else, any ideas please!

Chris

Code:
<?php

$dbcon = @mysql_connect("localhost", "user", "password") or die("Cannot connect to server");
if ($dbcon) { echo ("connected to server<br>");}

$db= @mysql_select_db("accom", $dbcon) or die("Cannot connext to DB");
if ($db) { echo ("connected to DB<br>");}

$fh =  fopen("/usr/home/testdata.txt","r");
while (list($place,$accom,$phone,$addy,$email) = fgetcsv($fh,10000)) {

ereg(',[[:space:]]*([^[:space:]]+),[[:space:]]*([[:alpha:]]{1,2}[[:digit:]]{1,2})....$',$addy,$county);

print "$place, $accom, $phone, $addy, $email, $county[1], $county[2]<br>";

$sql ="insert into test (town,county,area,accom,phone,addy,email)
Values (\"$place\",\"$county[1]\",\"$county[2]\",\"$accom\",\"$phone\",\"$addy\",\"$email\")";
if ($sql) { echo ("insert bit went ok<br>");}

$rs=mysql_query($sql,$dbcon);

if ($rs){ echo ("record $accom added <br><br>");}
else  {echo ("didn't work!<br><br>");}
}
?>


Posted by admin (Graham Ellis), 16 June 2003
Yep, looks like a case of "drop through"  

You're not checking that the ereg has actually worked - if  a match fails (e.g. the data is in the wrong format), then the $county variable is left uneffected - it is NOT cleared. My guess is that you have some incomplete postcodes in there - for example an SN13 rather than SN13 5ZZ.

Posted by Chris_Isaac (Chris Isaac), 17 June 2003
Hi

I've gone through it and think I've found the problem.  If the County contains spaces, the ereg uses the previous County that didn't eg, Newastle upon Tyne fails to be picked up.  How do I remove the spaces, I've tried a few options, but all are failing.

Code:
<?php

$dbcon = @mysql_connect("localhost", "user", "password") or die("Cannot connect to server");
if ($dbcon) { echo ("connected to server<br>");}

$db= @mysql_select_db("accom", $dbcon) or die("Cannot connext to DB");
if ($db) { echo ("connected to DB<br>");}

$fh =  fopen("/usr/home/testdata.txt","r");
while (list($place,$accom,$phone,$addy,$email) = fgetcsv($fh,10000)) {

ereg_replace(' ','-',$addy);
print "$addy<br><br>";

ereg(',[[:space:]]*([^[:space:]]+),[[:space:]]*([[:alpha:]]{1,2}[[:digit:]]{1,2})....$',$addy,$county);

if ($place AND $accom AND $phone AND $addy AND $email AND $county[1] AND $county[2]) {
     print "$place, $accom, $phone, $addy, $email, $county[1], $county[2] has been inserted<br>";

     $sql ="insert into test (town,county,area,accom,phone,addy,email)
     Values (\"$place\",\"$county\",\"$area\",\"$accom\",\"$phone\",\"$addy\",\"$email\")";
     if ($sql) { echo ("insert bit went ok<br>");}

     $rs=mysql_query($sql,$dbcon);

           if ($rs){ echo ("record $accom added <br><br>");}
           else  {echo ("didn't work!<br><br>");}
     }
     else { print "Something was missing !<br><br>";}
}
?>


Posted by admin (Graham Ellis), 17 June 2003
Try replacing

Code:
ereg(',[[:space:]]*([^[:space:]]+),[[:space:]]*([[:alpha:]]{1,2}[[:digit:]]{1,2})....$',$addy,$county);


by

Code:
ereg(',[[:space:]]*([^,]+),[[:space:]]*([[:alpha:]]{1,2}[[:digit:]]{1,2})....$',$addy,$county);


and PLEASE check the return status to see if it actually matched - if you don't, and if the match fails, then the data from the previous match will remain in $county and you know that it's gone wrong.

Posted by Chris_Isaac (Chris Isaac), 17 June 2003
Thank again, thats fixed it.

Chris

PS. I'll also try to post to the right topic section next time  



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.


WELL HOUSE CONSULTANTS LTD.: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho