2014-02-06 78 views
0

這是我正在使用的聲明。在codeigniter中插入聲明

$sql = "INSERT INTO store (shop_id, shop_index, items) VALUES ('$shop_id','65535','4')"; 
$this->db->query($sql); 
return shop_st; 

shop_st是自動遞增的主鍵,我想返回它。我想知道我的查詢是正確還是錯誤。請讓我知道如果我出錯了。

+2

要知道你的查詢是否是對還是錯,你必須執行它 –

+0

你不會到這裏來的錯誤'的回報shop_st'? –

回答

1

CI中的語法方面,如果啓用database幫手,你可以做這樣的事情:

$data = array(
    'shop_st' => NULL, 
    'shop_id' => $shop_id , 
    'shop_index' => '65535' , 
    'items' => '4' 
); 

$this->db->insert('mytable', $data); 

我不認爲除了你的方法,還有什麼比你的方法更「正確」,除非你能做到這一點。

return $this->db->insert_id(); //shop_st 

這將讓你最後插入的ID。

這個答案只是爲了讓你意識到codeigniter的內置助手。

數據庫幫助程序可以是found here

+0

謝謝亞歷克斯。我想返回shop_st。我不想讓它返回我shop_id。 – TULSIRAJ

+0

@ user3270582'insert_id'將是自動遞增的主鍵。所以這將是你正在尋找的那個。 ('shop_st') – Albzi

+0

是的。你的權利。 – TULSIRAJ

2

試試這個

$data = array(
    'shop_st' => NULL, 
    'shop_id' => $shop_id , 
    'shop_index' => '65535' , 
    'items' => '4' 
); 
function insertShop($data) 
{ 
$res=$this->db->insert('store', $data) 
if($res) 
{ 
    return $res; 
} 
else 
{ 
    return false; 
} 
}