我正在尝试从wpdb访问表,但是此GET 500错误一直出现。如果我在年份输入中未输入任何内容,那么它会随便打印“找不到年份”,这意味着ajax可以正常工作,但是$ wpdb变量出了点问题。如您所见,我尝试包括wp-db.php文件,但问题仍然存在。有人可以帮忙吗?预先感谢!
这是php代码:
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-includes/wp-db.php');
$s=(isset($_GET['username']) ? $_GET['username'] : '');
if ( $s != '' ) {
//global $wpdb;
$year = ( isset( $_GET['year'] ) ? $_GET['year'] : '');
if( $year != '' ) {
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM mytable WHERE year='2019'"));
echo $results;
}
else {
echo "year not found";
}
}
else echo "user not exist";
?>
这里是:
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<div id="data-container"><input id="year" type="text" name="year" placeholder="Year">
<button onclick="search()">Search</button>
</div>
<script>
function search() {
$.ajax({
url: "http://testingforwordpress.epizy.com/get_mylaw.php",
method:"GET",
data: { "username": getCookie("usernameCookie"), "year": $("#year").val() }
}).done(function(data) {
console.log(data);
});
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
</script>
答案 0 :(得分:0)
请不要执行wp-includes/wp-db.php
,
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-load.php');
,它将启动WP核心,并加载配置和数据库对象。 wp-db.php不能用作独立的子模块,它可以自己获取wp-config.php配置。