所以我必须在这个插件中添加一个WHERE查询,我用于WordPress网站上的报告功能。我没有时间做任何事情,只是添加另一列并按该列中的值进行过滤,因为没有太多数据来管理每个更新。我添加的列的默认值为零,但我将添加新条目以表示添加新人的年份。但是,当我根据列值进行过滤时,整个查询会中断并且不会显示。我不知道为什么。以下是涉及其设置查询显示结果的部分。
<?php
$sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . "presidentsreport_breakdown WHERE list_id = " . $atts['list_id'];
$total_breakdowns = $wpdb->get_var($sql);
$sql = "SELECT p.person_id, p.name, p.notes, p.school_year, b.breakdown_id, b.name as breakdown, b.description as breakdown_description FROM " . $wpdb->prefix . "presidentsreport_person p INNER JOIN " . $wpdb->prefix . "presidentsreport_breakdown b ON b.breakdown_id = p.breakdown_id INNER JOIN " . $wpdb->prefix . "presidentsreport_list l ON l.list_id = b.list_id";
$clean_where = " WHERE l.list_id = " . $atts['list_id'];
$where = "";
if($search != ''){
$where = " AND (p.name LIKE %s)";
$arg = '%' . $search . '%';
$args = array($arg);
}
$where = $wpdb->prepare($where, $args);
$order = " ORDER BY b.sort_order, b.breakdown_id, p.sort_name, p.name, p.person_id";
$results = $wpdb->get_results($sql . $clean_where . $where . $order);
?>
如果我在变量$中添加任何内容,它会破坏整个查询。所以,如果我添加
<?php
$where = " WHERE p.school_year <= 2011";
?>
或
<?php
$where = " WHERE p.school_year = 0";
?>
没有任何内容会显示,对于最后一个示例,如果默认值为0,则无论如何都应该显示。提前感谢您阅读!
答案 0 :(得分:2)
不要将WHERE
添加到您的变量中。它已在$clean_where
$clean_where = " WHERE l.list_id = " . $atts['list_id'];
Here ------------^
您需要将添加参数连接到$where
变量:
$where .= " AND p.school_year <= 2011";
答案 1 :(得分:0)
在哪里不需要WHERE!