0

SOLVED. SEE BELOW.

Initial question:

I need to extract lat and lon from an array field (643844X2X48) in a mySQL table where the data look like this:

56.559482483762245;-104.0625;;SK;CA;S0J

I tried mysqli and pdo code and failed.

This php/mysql code updates all of the lat fields with the final loop lat value:

$sql = "SELECT 643844X2X48 from `reg_survey_643844`";
$array = mysql_query($sql);
while($row=mysql_fetch_assoc($array)){
foreach($row as $value){
    $result= explode(";", $value);
    $slice= (array_slice($result,0,1)); 
    $lat = array_shift(array_values($result));
    mysql_query("UPDATE reg_survey_643844 SET lat = '$lat'");
    }
    }

What am I doing wrong?

UPDATE:

Here is the working code.

$query = "SELECT * FROM reg_survey_643844 ORDER BY id";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
        $resultGeo= explode(";", $row["643844X2X48"]);
        $lat = $resultGeo[0];
        $lon = $resultGeo[1];
        $city = $resultGeo[2];
        $state = $resultGeo[3];
        $countryCode = $resultGeo[4];
        $zip = $resultGeo[5];
        $getid = $row['id'];
        $siteName = $row['643844X2X12'];

        $query = mysql_query("UPDATE reg_survey_643844 
                        SET siteName = '$siteName', lat = '$lat', lon = '$lon', city = '$city', state = '$state', countryCode = '$countryCode', zip = '$zip'
                   WHERE id = '$getid'");

        }

1 Answer 1

1

Try this

 $sql = "SELECT 643844X2X48 from `reg_survey_643844`";
    $array = mysql_query($sql);
    while($row=mysql_fetch_assoc($array)){

    $result= explode(";", $row["your data"]);

    $lat = $result[0];
    mysql_query("UPDATE reg_survey_643844 SET lat = '$lat' whrere  your conditions");

    }
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer, vijay4vijju. Much appreciated. I'm getting the same result, however. Quick question: by "your data" do you mean the text field column containing the georeferencing array data? That's what I used. I tried a few where conditions, but nothing seemed to work.
Thanks vijay4vijju! With your guidance, I was able to get working code. Updated my question with it. Muchas gracias!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.