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
看起來是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();
}
那個,有件事不知道該不該說。
為何直接用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;
}