Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

You can Add a comment or ranking to this page

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