我的查询没有显示任何内容

时间:2017-01-02 20:13:08

标签: php mysql

我使用php,mysql从我的数据库中检索数据,既没有显示错误也没有给我所需的数据。

public function last_material_code($department_id){
    $query = $this->db->prepare("SELECT material_code FROM materials_tbl WHERE dept_id = $department_id");
    return $query -> execute();
}

<?php
include_once ('select.class.php');

if(isset($_POST['department_id'])){
    $department_id = $_POST['department_id'];

    $object = new select_class($DB_con);
    $object -> last_material_code($department_id);
}
?>

2 个答案:

答案 0 :(得分:0)

您应该返回提取的结果$query->fetch()

答案 1 :(得分:0)

这是我后来在我的功能中所做的,它起作用了......

public function last_material_code($department_id){
$query = $this->db->prepare("SELECT  material_code FROM materials_tbl
    WHERE dept_id = $department_id ORDER BY Id DESC LIMIT 1");
    $query -> execute();
    if ($query){
        while($row=$query->fetch(PDO::FETCH_ASSOC)){  
            echo $row["material_code"];
        }
    }
}