更改数据库连接以连接到Wordpress连接 - 应该很简单吗?

I hope someone can help me, I am trying to write the following php script to connect with wordpress db connection instead of connecting via a custom connection.

This is the original script with the custom connection

    <?php
require("phpsqlsearch_dbinfo.php");

// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];

// Start XML file, create parent node
$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);

// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}

// Search the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($center_lng),
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($radius));
$result = mysql_query($query);

$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

header("Content-type: text/xml");

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['name']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("distance", $row['distance']);
}

echo $dom->saveXML();
?>

This is how I currently have the script to connect to the db using the wordpress connection

     <?php 
    /* I have added this require as suggested in the replies below and the page seem to 
be connecting correctly to the wp database*/
    require('../../../../wp-blog-header.php');
        /* Get parameters from URL*/
        $center_lat = $_GET["lat"];
        $center_lng = $_GET["lng"];
        $radius = $_GET["radius"];

        /*Start XML file, create parent node*/
        $dom = new DOMDocument("1.0");
        $node = $dom->createElement("markers");
        $parnode = $dom->appendChild($node);

        /* Search the rows in the markers table*/
        global $wpdb;
        /*$query = $wpdb->get_results("SELECT * FROM customers;");*/
/*I have removed the $wpdb->sprint below and just used sprint as per comments below - it seems to work*/        
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM wp_markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
          mysql_real_escape_string($center_lat),
          mysql_real_escape_string($center_lng),
          mysql_real_escape_string($center_lat),
          mysql_real_escape_string($radius));
        $result = mysql_query($query);

        if (!$result) {
          die("Invalid query: " . mysql_error());
        }

        header("Content-type: text/xml");

        /* Iterate through the rows, adding XML nodes for each*/
        while ($row = @mysql_fetch_assoc($result)){
          $node = $dom->createElement("marker");
          $newnode = $parnode->appendChild($node);
          $newnode->setAttribute("name", $row['name']);
          $newnode->setAttribute("address", $row['address']);
          $newnode->setAttribute("lat", $row['lat']);
          $newnode->setAttribute("lng", $row['lng']);
          $newnode->setAttribute("distance", $row['distance']);
        }

        echo $dom->saveXML();
        ?>

I have basically removed this part from the first script

require("phpsqlsearch_dbinfo.php");

and also removed this part

// Opens a connection to a mySQL server
$connection=mysql_connect (localhost, $username, $password);
if (!$connection) {
  die("Not connected : " . mysql_error());
}

// Set the active mySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ("Can\'t use db : " . mysql_error());
}

and added this part which I believe is connecting to the wordpress db

    global $wpdb;
$query = $wpdb->sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM wp_markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",

However now I get the following error when I query the page

Fatal error: Call to a member function sprintf() on a non-object in

The error is on this line

  $query = $wpdb->sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM wp_markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",

What do I need to do to correctly convert the code to connect to the wordpress db - It seems to connect to the db now, but the xml is not getting created from the query... I believe there is still an error somewhere in this code

while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['name']);
  $newnode->setAttribute("address", $row['address']);
  $newnode->setAttribute("lat", $row['lat']);
  $newnode->setAttribute("lng", $row['lng']);
  $newnode->setAttribute("distance", $row['distance']);
}

You have to load Wordpress bootstrap first in order to use $wpdb. Try adding code:

include('wp-load.php');

before

global $wpdb;