Using db_query in Drupal 7

4 years 8 months ago

Quick script showing how to use db_query in drupal 7.

Also shown is how to get the location module to sort by distance using "Use PHP code to determine latitude/longitude" given a zip code argument.

$zip = '';
if (isset($_GET['zip'])){
  $zip = $_GET['zip'];

$result = db_query("SELECT latitude,longitude FROM zipcodes where zip = '".$zip."'");

foreach ($result as $record) {
  $lat = $record->latitude;
  $long = $record->longitude;

$prox = array('latitude' => $lat, 'longitude' => $long); return $prox;