0

我希望有人可以帮助我,我正在尝试编写以下 php 脚本来连接 wordpress db 连接,而不是通过自定义连接进行连接。

这是带有自定义连接的原始脚本

    <?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();
?>

这就是我目前使用wordpress连接连接到数据库的脚本的方式

     <?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();
        ?>

我基本上已经从第一个脚本中删除了这部分

require("phpsqlsearch_dbinfo.php");

并删除了这部分

// 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());
}

并添加了我认为连接到 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",

但是现在我在查询页面时收到以下错误

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

错误在这一行

  $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",

我需要做什么才能正确转换代码以连接到 wordpress db - 现在似乎连接到 db,但是没有从查询中创建 xml ......我相信这其中的某个地方仍然存在错误代码

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']);
}
4

1 回答 1

1

您必须先加载 Wordpress 引导程序才能使用$wpdb. 尝试添加代码:

include('wp-load.php');

global $wpdb;
于 2013-08-27T09:08:59.853 回答