php - Why can't I get data after executing the encapsulated mysql statement?
phpcn_u1582
phpcn_u1582 2017-05-16 12:59:06
0
3
546
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; ?>

Please tell me why Array is returned in HTML. You can get the result when executing in SQL. The SQL statement is correct.
Under each specific page, the corresponding SQL statement can be printed and the assignment variable is correct. I don’t know why it just returns Array

phpcn_u1582
phpcn_u1582

reply all(3)
漂亮男人

Maybe it’s because you didn’t 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);
    }

Try this

I think you can try to solve it like this

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'])));

Regarding the query result being Array, it may be query_all(). There is a problem with this method. Please check if there is a function similar to query_one()

为情所困

Change echo to var_dump or print_r. Echo cannot print array structures.

仅有的幸福

The get_avgprice_item() function has no return. When the function method does not return any variables or methods, the system defaults to the function and method returning null
And if you look at $this->query_all($sql); there is no assignment after query Give any variables and no return parameters, the correct way

 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);
    }
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template