gpt4 book ai didi

php - 不确定在 codeigniter 中将 where 子句放在哪里

转载 作者:行者123 更新时间:2023-11-29 12:25:38 24 4
gpt4 key购买 nike

我一直在查看 codeigniters 站点上的事件记录指南,我试图找出将 where 子句放在模型或 Controller 中的位置?我认为它包含在模型中,但不确定如何实现它。 $where = "EmpName='Donny"; $this->db->where($where);

名为 Home.php 的 Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller
{
public function index()
{

$this->load->model('HomeModel');
$data['records'] = $this->HomeModel->getData();
$this->load->view('HomeView',$data);
}
}

HomeModel.php

<?php 

class HomeModel extends CI_Model{

public function getData(){

$query = $this->db->get('requests');
return $query->result();
}

}

HomeView.php

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home View</title>
</head>
<body>
<h1>Our DB Results:</h1>
<?php

foreach($records as $r){

echo $r->EmpName." ".$r->Department."<br>";

};?>

</body>
</html>

最佳答案

试试这个

$query = $this->db->get_where('requests', array('id' => $id));

你可以使用

$this->db->where('empname', 'Donny'); 

关于php - 不确定在 codeigniter 中将 where 子句放在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28365531/

24 4 0