Hi I have a postcode database which contains postcodes and the lat and lon details, In another table i have a list of companies with postcodes and want to add the lat and lon details to matching postcodes, rather than use a join query etc. Below is the code i am using, i have used something similar before, but i just can not work out why this isn’t working.
$host = "localhost";
$username = "";
$password = "";
$database = "";
mysql_connect( $host, $username, $password );
mysql_select_db( $database ) or die( "Unable to select database" );
$query = "SELECT * FROM postcodes";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$postcode = $row['postcode'];
$lat = $row['lat'];
$lon = $row['lon'];
$result = mysql_query("UPDATE companies SET lat ='$lat' and lng ='$lon'
WHERE postal_code = '$postcode'")
or die(mysql_error());
}
The error I am getting is: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/visitrac/public_html/postcode.php on line 15
if i move the closed bracket so it is before the update then the error goes but no information gets into the database, which i assume is because it is out of the loop. I have tried all sorts of things but nothing seems to work and so any nudges in the right direction would be appreciated.
Thanks
There’s really no need for the loop through the postcode results. You can update all companies in a single SQL statement.