iT邦幫忙

0

如何搜尋資料表內的內容 運用CI框架

modle的function
function getjudge($s_name,$p_name)
{
$query = $this->db->query('SELECT judge FROM student WHERE s_name=$s_name AND p_name=$p_name');

foreach ($query->result() as $row)
{
        echo $row->judge;
}	
echo $query->num_rows();
}
controller的function
function login()

{
$this->load->helper('url');
$this->load->library('session');
$s_name = trim($this->input->post("s_name"));
$p_name = trim($this->input->post("p_name"));
$this->load->model('MStudent','',TRUE);
$user = $this->MStudent->getUser($s_name,$p_name);
$j= $this->MStudent->getjudge($s_name,$p_name);
$judge= is_string($j);
$this->session->set_userdata('user',$s_name);
if($user != null)
{
echo $judge;
}

else//登入失敗
{
  echo 'login error';
}   

出現錯誤:
Error Number: 1054

Unknown column '$s_name' in 'where clause'

SELECT judge FROM student WHERE s_name=$s_name AND p_name=$p_name

Filename: C:/wamp64/www/system/database/DB_driver.php

Line Number: 691

2 個回答

0
dragonH
iT邦新手 5 級 ‧ 2019-05-14 16:10:21

看起來是sql查詢的問題

我很久以前寫的給你參考

	public function ShowPost($Bid,$Aid)
	{
		$sql="select * from posts Where Bid=? AND Aid=?";
        $query = $this->db->query($sql, array($Bid, $Aid));
		return $query->result_array();
    }
0
浩瀚星空
iT邦高手 1 級 ‧ 2019-05-14 16:40:52

那個,有件事不知道該不該說。
為何直接用query處理。
都用ci的db框架了。怎麼還在用query。
基本教學學一下吧。

$query = $this->db->query('SELECT judge FROM student WHERE s_name=$s_name AND p_name=$p_name');
foreach ($query->result() as $row)
{
        echo $row->judge;
}	

可以改成如下處理

$getData = $this->db->select('judge')
->where('s_name',$s_name)
->where('p_name',$p_name)
->get('student')->result_array();
foreach($getData AS $row){
   echo $row['judge'];
}

如要物件化的做法則如下處理

$getData = $this->db->select('judge')
->where('s_name',$s_name)
->where('p_name',$p_name)
->get('student')->result();
foreach($getData AS $row){
   echo $row->judge;
}
小魚 iT邦高手 1 級 ‧ 2019-05-14 16:48:44 檢舉

跟 C# 的 Linq 有點像...

dragonH iT邦新手 5 級 ‧ 2019-05-14 17:07:31 檢舉

官方教我的(遮臉

當初不知道哪個筋不對
現在的話一定秒選浩瀚大說的這種寫法/images/emoticon/emoticon01.gif

我要發表回答

立即登入回答