drtzb06222 2016-11-11 22:58
浏览 49
已采纳

使用DB2从autocomplete jquery UI返回的未定义值

I have the following code that works like a charm with mysql but when implemented using DB2, it returns value "undefined" where should be the actual value.

consult.php

require_once("connect_db.php");

$action = (isset($_GET['action'])) ? $_GET['action'] : '';
$param = (isset($_GET['parameter'])) ? $_GET['parameter'] : '';

if($action == 'autocomplete'):
    $where = (!empty($parameter)) ? "WHERE name LIKE '%{$parameter}%'" : "";
    $sql = "SELECT LOWER(name) FROM categories " . $where;

    $stmt = db2_prepare($connection, $sql);

        db2_execute($stmt);
        $data = db2_fetch_object($stmt);

    $json = json_encode($data);
    echo $json;
endif;

categories.js

$(function() {

$( "#category" ).autocomplete({
        minLength: 1,
        source: function( request, response ) {
            $.ajax({
                url: "consult.php",
                dataType: "json",
                data: {
                        action: 'autocomplete',
                    parameter: $('#category').val()
                },
                success: function(data) {
                   response(data);
                }

            });
        },

})
.autocomplete( "instance" )._renderItem = function( p, item ) {
  return $( "<p>" )
    .append( "<a><b>" + item.name + "</b></a>" )
    .appendTo( p );
};

});
  • 写回答

1条回答 默认 最新

  • doumanju2533 2016-11-12 13:40
    关注

    There was an error in my code. I had forgotten to fetch all data as below:

        $stmt = db2_prepare($connection, $sql);
    
            db2_execute($stmt);
    
        while($row = db2_fetch_assoc($stmt)) {
                $data[] = array(
                'name' => $row['NAME']
                );
        } 
    

    Now it is working fine

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么