gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:38:23 25 4
gpt4 key购买 nike

我希望有人可以帮助我,我正在尝试编写以下 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 数据库的部分

    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 数据库 - 现在似乎已连接到数据库,但 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']);
}

最佳答案

您必须首先加载 Wordpress Bootstrap 才能使用 $wpdb。尝试添加代码:

include('wp-load.php');

之前

global $wpdb;

关于php - 更改数据库连接以连接到 Wordpress 连接 - 应该很简单吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18460941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com