mackin Posted November 16, 2011 Share Posted November 16, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/251245-empty-array/ Share on other sites More sharing options...
Adam Posted November 16, 2011 Share Posted November 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251245-empty-array/#findComment-1288654 Share on other sites More sharing options...
cypher86 Posted November 16, 2011 Share Posted November 16, 2011 echo $sqlstring2 and run it on the db in order to see if it returns results. Quote Link to comment https://forums.phpfreaks.com/topic/251245-empty-array/#findComment-1288655 Share on other sites More sharing options...
nethnet Posted November 16, 2011 Share Posted November 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/251245-empty-array/#findComment-1288657 Share on other sites More sharing options...
mackin Posted November 16, 2011 Author Share Posted November 16, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/251245-empty-array/#findComment-1288659 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.