1

我在我的二十二模板中将带有 ajax 的变量传递给 list.php。在 list.php 我正在执行 mysql 选择查询,但是当我在控制台中看到时,我收到了这个错误:

<br />
<b>Fatal error</b>:  Call to a member function get_results() on a non-object in <b>D:\xampp\htdocs\wordpress\wp-content\themes\twentytwelve\list.php</b> on line <b>4</b><br />


我的代码:

搜索.php

<?php
/*
Template Name: Search
*/
get_header();?>

<script type="text/javascript" src="<?php echo get_template_directory_uri(); ?>/js/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
  $("#parent_category").change(function()
  {
    var parent_category = $(this).val();
    if(parent_category != '')  
     {
      $.ajax
      ({
         type: "POST",
         url: "<?php echo get_template_directory_uri(); ?>/list.php",
         data: "parent_category="+ parent_category,
         success: function(option)
         {
           $("#child_category").html(option);
         }
      });
     }
     else
     {
       $("#child_category").html("<option value=''>-- No category selected --</option>");
     }
    return false;
  });
});
</script>

<select id="parent_category" name="parent_category">
    <option value="" selected="selected">-- Select blood group --</option>
    <option value="A1 positive">A1 positive</option>
    <option value="A1 negative">A1 negative</option>
    <option value="A2 positive">A2 positive</option>
    <option value="A2 negative">A2 negative</option>
    <option value="B positive">B positive</option>
    <option value="B negative">B negative</option>
    <option value="A1B positive">A1B positive</option>
    <option value="A1B negative">A1B negative</option>
    <option value="A2B positive">A2B positive</option>
    <option value="A2B negative">A2B negative</option>
    <option value="AB positive">AB positive</option>
    <option value="AB negative">AB negative</option>
    <option value="O positive">O positive</option>
    <option value="O negative">O negative</option>
    <option value="A positive">A positive</option>
    <option value="A negative">A negative</option>
</select>

<select id="child_category" name="child_category">
  <option value="">-- No location selected --</option>
</select>

<?php get_footer(); ?>


列表.php

<?php
if(isset($_POST['parent_category']) && $_POST['parent_category'] != '')
{   
    $result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );
    print_r($result);   
}
?>


有什么想法或建议吗?谢谢。

4

1 回答 1

2

您必须wp-blog-header在您的list.phpthen 定义$wpdbglobal包含错误,因为您的错误是由于$wpdb未定义的定义

<?php include("yourpath/wp-blog-header.php");
 global $wpdb;
if(isset($_POST['parent_category']) && $_POST['parent_category'] != '')
{   
    $result = $wpdb->get_results( "SELECT home_location FROM wp_places WHERE blood_group LIKE '".$getGroupType."%'" );
    print_r($result);   
}
?>
于 2013-07-01T17:53:56.687 回答