【问题标题】:Wordpress SQL Select ErrorWordpress SQL 选择错误
【发布时间】:2016-04-01 06:51:31
【问题描述】:

我正在开发 WordPress 上的插件,以帮助我跟踪我的日程安排和任务。我知道有很多插件可以做这些事情。但我这样做是为了学习如何在 WordPress 上创建插件。

我正在数据库上执行这条 SQL 指令Select * From wp_jrmr_calendar Where fecha_inicio between '2016-03-01' and '2016-5-01'。我有 5 行匹配。我直接在数据库上执行 SQL 指令并返回结果:

    <table name="wp_jrmr_calendar">
        <!-- Tabla wp_jrmr_calendar -->
        <tr name="wp_jrmr_calendar">
            <td name="id">1</td>
            <td name="fecha_inicio">2016-03-30</td>
            <td name="hora">00:20:16</td>
            <td name="duracion">1983</td>
            <td name="titulo">2016-03-30</td>
            <td name="descripcion">2016-03-30</td>
            <td name="todo_el_dia">N</td>
            <td name="tipo_evento">GENERAL</td>
        </tr>
        <tr name="wp_jrmr_calendar">
            <td name="id">2</td>
            <td name="fecha_inicio">2016-03-30</td>
            <td name="hora">08:00:00</td>
            <td name="duracion">0.5</td>
            <td name="titulo">dddd</td>
            <td name="descripcion"></td>
            <td name="todo_el_dia">N</td>
            <td name="tipo_evento">GENERAL</td>
        </tr>
        <tr name="wp_jrmr_calendar">
            <td name="id">3</td>
            <td name="fecha_inicio">2016-03-30</td>
            <td name="hora">08:00:00</td>
            <td name="duracion">0.5</td>
            <td name="titulo">dddd</td>
            <td name="descripcion"></td>
            <td name="todo_el_dia">N</td>
            <td name="tipo_evento">GENERAL</td>
        </tr>
        <tr name="wp_jrmr_calendar">
            <td name="id">4</td>
            <td name="fecha_inicio">2016-03-31</td>
            <td name="hora">00:00:00</td>
            <td name="duracion">0.5</td>
            <td name="titulo">Otra Entrada nueva</td>
            <td name="descripcion"></td>
            <td name="todo_el_dia">Y</td>
            <td name="tipo_evento">GENERAL</td>
        </tr>
        <tr name="wp_jrmr_calendar">
            <td name="id">5</td>
            <td name="fecha_inicio">2016-04-04</td>
            <td name="hora">23:30:00</td>
            <td name="duracion">1.5</td>
            <td name="titulo">Recoger Mónica en Aeropuerto</td>
            <td name="descripcion"></td>
            <td name="todo_el_dia">N</td>
            <td name="tipo_evento">GENERAL</td>
        </tr>
    </table>

但是 WordPress 给了我这个错误:

错误 de la base de datos de WordPress para la consulta Select * From wp_jrmr_calendar Where fecha_inicio 在 '2016-03-01' 和 '2016-05-01' realizada por do_action('wp_ajax_search_calendar'), call_user_func_array, HostelApp\Calendar \CalendarManager->search_calendar, HostelApp\Calendar\Calendar->getCalendar.

我知道数据库查询是正确的,但是我不知道还要寻找什么来找到错误。

查询是由Javascript上的这个函数触发的

function search_events (year, month){
		 
		jQuery.ajax({ 
			type: 'post',
			url: js_object.ajaxurl,
			async: false,
			data:{
			        'action': 'search_calendar',
			        'year': year,
			        'month': month
			}, 
			success: function(response){
			    	jQuery('section[role="error_message"]').empty();
			    	if (response.success){
			    		listado_eventos = response.data;
			    	}
			    	else {
			    		jQuery('section[role="error_message"]').append(
			    				'<article class="alert alert-danger">'+response.data+'</article>');
			    	}
			    }
		});
		return listado_eventos;
	}
并由这两个函数在php上执行

	public function search_calendar(){
		$year = $this->param('year');
		$moth = $this->param('month');
		
		if ((!$year) || (!$moth)){
			wp_send_json_error('Los campos de mes y año son obligatorios ');
		}
		else{
			$mothnext = $moth+1;
			if ($moth < 10) $moth = "0".$moth;
			if ($mothnext < 10) $mothnext = "0".$mothnext;
			$where = "fecha_inicio between '".$year."-".$moth."-01' and '".$year."-".$mothnext."-01'";
			$items = $this->calendar->getCalendar($where);
			if (count($this->calendar->getErrorMsgs())>0){
				wp_send_json_error($this->calendar->getErrorMsgsAndClean());
			}
			else wp_send_json_success($items);
		}
	}

public function getCalendar($where){
        global $wpdb
		$sql = 'Select * From '.$wpdb->prefix.$this->tablename;
		if (!empty($where))
			$sql =$sql." Where ".$where;
		$items = $wpdb->get_results($sql);
		if (!empty($wpdb->print_error()))
			$this->setErrorMsg('Search Operation error: '.$wpdb->print_error());
		
		return $items;
	}

但真正奇怪的想法是查询只返回查询的最后一行。

【问题讨论】:

  • 您应该发布代码而不是一些似乎与您的问题无关的 html。
  • 您好,我正在使用触发 sql 查询的 php 和 javascript 代码更新帖子。
  • 您好,我找到了解决办法。将 where 子句从 '2016-03-01' 和 '2016-04-01' 之间的 fecha_inicio 更改为 fecha_inicio >= '2016-03-01' 和 fecha_inicio

标签: php mysql wordpress


【解决方案1】:

global $wpdb 缺少分号(;)

public function getCalendar($where){
        global $wpdb;
        $sql = 'Select * From '.$wpdb->prefix.$this->tablename;
        if (!empty($where))
            $sql =$sql." Where ".$where;
        $items = $wpdb->get_results($sql);
        if (!empty($wpdb->print_error()))
            $this->setErrorMsg('Search Operation error: '.$wpdb->print_error());

        return $items;
    }

案例2:可能是你的条件匹配。所以,它给你一个列。

【讨论】:

  • 感谢您的回答。但是分号错误是我的错,我在帖子上翻译函数时犯了这个错误。
猜你喜欢
  • 1970-01-01
  • 2016-01-14
  • 1970-01-01
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
  • 2014-01-12
  • 1970-01-01
相关资源
最近更新 更多