[英]Wordpress using $_GET in sql query
I am trying to run a "sql query" which needs a post result. 我正在尝试运行需要发布结果的“sql查询”。 I keep getting this error: 我一直收到这个错误:
Call to a member function get_results() on a non-object in ../includes/functions.php on line 43
Here is my code: 这是我的代码:
if (isset($_GET['slug'])){
function get_slug() {
$slug = 'home';
return $slug;
}
} else {
function get_slug() {
$sql = "SELECT slug FROM wp_content WHERE slug='".$_GET['slug']."'";
while ($result = $wpdb->get_results($sql)) {
$slug = $result['slug'];
return $slug;
}
}
}
I think that the problem is in the $_GET
. 我认为问题在于$_GET
。 I've tried the "wordpress function" get_query_var('slug')
instead and still the same error occurs. 我已经尝试过“wordpress function” get_query_var('slug')
,但仍会出现相同的错误。
Here is the modified code. 这是修改后的代码。 i hope it will work. 我希望它能奏效。 if not let me know. 如果没有让我知道。
if (isset($_GET['slug'])){
function get_slug() {
$slug = 'home';
return $slug;
}
} else {
function get_slug() {
global $wpdb;
$sql = "SELECT slug FROM wp_content WHERE slug='".$_GET['slug']."'";
while ($result = $wpdb->get_results($sql)) {
$slug = $result['slug'];
return $slug;
}
}
}
Its because, $wpdb;
因为, $wpdb;
is not accessible from inside your function. 无法从您的函数内部访问。
If you add a language construct global
to $wpdb
, you will get this object. 如果将一个语言结构global
添加到$wpdb
,您将获得此对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.