-1

So I have to add a WHERE query to this plugin I'm using for a reporting feature on a WordPress site. I have no time to do anything but add in another column and filter by the values in that column as there is not that much data to manage each update. The default value for the column I added is zero but I'll add new entries to represent years new people are added. However, when I filter based on the column value the whole query breaks and doesn't show up. I have no idea why. Here is the section involving its set up query displaying results.

<?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);

?>

If I add anything in the variable $where it breaks the whole query. So if I add

<?php

$where = " WHERE p.school_year <= 2011";

?>

or

<?php

$where = " WHERE p.school_year = 0";

?>

Nothing will show up, For the last example if the default value is 0 everything should show up regardless. Thanks in advance for reading through!

4

2 回答 2

2

不要添加WHERE到您的变量中。它已经分配在$clean_where

$clean_where = " WHERE l.list_id = " . $atts['list_id'];
Here ------------^

您需要将您的附加参数连接到$where变量:

$where .= " AND p.school_year <= 2011";
于 2013-11-06T22:31:15.580 回答
0

不需要 WHERE 在哪里!

于 2013-11-06T22:36:50.393 回答