Jump to content

empty array??


mackin

Recommended Posts

I am using a script to take postcodes and give distances between them and list all postcodes within a radius. Where it should list all the postcodes in the radius it provides and empty array - any clues??

 

the table is 'geodb'

 

relevant fields are    

postcode 

place 

latitude 

longitude

 

and the code is

if(isset($_GET['Search']))
{
    $postcode = strtoupper($_GET['postcode']);
    $radius = $_GET['radius'];
echo $postcode . '<br>';
echo $radius . '<br>';

$conn = mysql_connect(/*credentials removed*/) or die('db connect error: ' . mysql_error());
    mysql_select_db('db379121', $conn) or die('could not select database');
  $sqlstring = "SELECT * FROM geodb WHERE postcode = '".$postcode."'";
    $result = mysql_query($sqlstring);

    $row = mysql_fetch_assoc($result);

++++++++++++++++++++++++++++++++++++++++++
var_dump($row); // seeing whqat is in the array - returns one result (array(9) { ["id"]=> string(1) "1" ["countrycode"]=> string(2) "GB" ["postcode"]=> string(4) "AB10" ["place"]=> string( "Aberdeen" ["postaltown"]=> string( "Aberdeen" ["region1"]=> string( "Scotland" ["region2"]=> string(13) "Aberdeen City" ["latitude"]=> string(12) "57.135813090" ["longitude"]=> string(12) "-2.121224030" })//
++++++++++++++++++++++++++++++++++++++++++	
    $lng = $row["longitude"] / 180 * M_PI;
    $lat = $row["latitude"] / 180 * M_PI;
echo $lat .
'<br>';
echo $lng . '<br>';
++++++++++++++++++++++++++++++++++++++++++
echo to check that there is a value here - i get lat  0.99720805922458        lng -0.037022343495923
++++++++++++++++++++++++++++++++++++++++++
    mysql_free_result($result);}



$sqlstring2 = "SELECT DISTINCT geodb.postcode,geodb.place,(6367.41*SQRT(2*(1-cos(RADIANS(geodb.latitude))*cos(".$lat.")*(sin(RADIANS(geodb.longitude))*sin(".$lng.")+cos(RADIANS(geodb.longitude))*cos(".$lng."))-sin(RADIANS(geodb.latitude))* sin(".$lat.")))) AS Distance FROM geodb AS geodb WHERE (6367.41*SQRT(2*(1-cos(RADIANS(geodb.latitude))*cos(".$lat.")*(sin(RADIANS(geodb.longitude))*sin(".$lng.")+cos(RADIANS(geodb.longitude))*cos(".$lng."))-sin(RADIANS(geodb.latitude))*sin(".$lat."))) <= '".$distance."') ORDER BY Distance";

    $result = mysql_query($sqlstring2) or die('query failed: ' . mysql_error());

    $str = "<table width=\"300\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
    $str .= "<tr>";
    $str .= "<th>postcode</th>";
    $str .= "<th>city</th>";
    $str .= "<th>distance</th>";
    $str .= "</tr>";

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        $str .= "<tr><td>".$row["postcode"]."</td><td>".$row["place"]."</td><td>".round($row['Distance'])."km</td></tr>";
    }

    $str .= "</table>";
++++++++++++++++++++++++++++++++++++++++++
var_dump($row["place"]); // this produces null
++++++++++++++++++++++++++++++++++++++++++

    mysql_free_result($result);
    mysql_close($conn);
    echo $str;	

Link to comment
https://forums.phpfreaks.com/topic/251245-empty-array/
Share on other sites

Your while condition above is to keep looping "while $row = a row from the database". So when the while loop has finished, it's because mysql_fetch_array has returned null because there are no more rows, subsequently meaning that $row now contains null.. So basically what you're getting back is correct.

Link to comment
https://forums.phpfreaks.com/topic/251245-empty-array/#findComment-1288654
Share on other sites

As Adam has said, you can only access the values of $row while you are within your loop.. trying to do so outside of that scope will produce null then the array is, at that point, null.  You could try building a second array of just these "place" values that you obtain inside the loop, which will then persist outside of its scope.

Link to comment
https://forums.phpfreaks.com/topic/251245-empty-array/#findComment-1288657
Share on other sites

ah i see that about echoing $row - and that was just to see if anything was there - and i see that was wrong - changed is to var_dump($str) now and i get string(126) "" - what does this mean.

 

I ran the query in phpmyadmin and it returns no results

 

i am thinking its a problem with the 2nd query

Link to comment
https://forums.phpfreaks.com/topic/251245-empty-array/#findComment-1288659
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.