搜尋

首頁  >  問答  >  主體

php - 為什麼封裝的mysql語句執行後得不到資料

model文件:
    public function get_avgprice_item($category_id){
        $sql = "SELECT AVG(yj) FROM " . $this->get_table('item') . ' where category_id = ' . intval($category_id);
         return $this->query_all($sql);
    }
main文件:
TPL::assign('avgprice', $this->model('item')->get_avgprice_item($category_info['id']));
htm文件:
<?php echo $avgprice; ?>

請教下,在html中為什麼返回Array,在sql執行可以得到結果,,sql語句沒錯
在每個特定的頁面下面,能印出對應的sql語句,賦值變數也沒錯,不知道為什麼就是返回Array

phpcn_u1582phpcn_u15822833 天前550

全部回覆(3)我來回復

  • 漂亮男人

    漂亮男人2017-05-16 13:01:06

    可能是你沒有return吧

        public function get_avgprice_item($category_id){
            $sql = "SELECT AVG(yj) FROM " . $this->get_table('item') . ' where category_id = ' . intval($category_id);
            return $this->query_all($sql);
        }

    這樣試試

    我覺得你可以嘗試這樣解決

    step 1.

        public function get_avgprice_item($category_id){
            $sql = "SELECT AVG(yj) FROM " . $this->get_table('item') . ' where category_id = ' . intval($category_id);
            return $sql;
        }

    step 2.

    TPL::assign('avgprice', $this->model('item')->query_all(get_avgprice_item($category_info['id'])));

    關於查詢結果是Array 可能是query_all() 這個方法有問題,你看一下是否可能還有類似 query_one() 這樣的函數

    回覆
    0
  • 为情所困

    为情所困2017-05-16 13:01:06

    將echo改為var_dump或print_r,echo並不能列印數組結構。

    回覆
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 13:01:06

    get_avgprice_item() 函數沒有return,當函數方法沒有return任何變數和方法的時候,系統預設該函數和方法傳回的是null
    而且你看看$this->query_all($sql); 查詢後沒有賦值給任何變量,也沒return參數,正確的方法

     public function get_avgprice_item($category_id){
            $sql = "SELECT AVG(yj) FROM " . $this->get_table('item') . ' where category_id = ' . intval($category_id);
            return $this->query_all($sql);
        }

    回覆
    0
  • 取消回覆