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

时间:2013-08-27 08:47:42

标签: php mysql wordpress

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

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

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

1 个答案:

答案 0 :(得分:1)

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

include('wp-load.php');

global $wpdb;