[英]What's wrong with my CodeIgniter SQL simple Query?
我只是對一個讓我瘋狂的查詢有一點疑問。 我正在使用CI,這是我第一次使用這個偉大的框架
1-好的,所以我有一個插入查詢工作得相當好:
$data = array(
'nom' => $nom,
'prenom' => $prenom,
'login' => $prenom.' '.$nom,
'password' => 'facebook',
'email' => $fb_data['me']['email'],
'mobile' => "00",
'etat' => "1",
'role' => "1",
'ville' => 'ville actuelle',
'facebook_id' => $fb_data['uid'],
);
$this->db->insert('membre', $data);
我不明白為什么它總是插入數據兩次......!
2-然后我得到了第二個問題:我只想找到一個有相關facebook_id的用戶,所以我試試:
$this->db->select('nom');
$this->db->from('membre');
$this->db->where('facebook_id',$fb_data['uid']);
$resultat=$this->db->get();
echo '<pre>';
print_r($resultat);
echo '</pre>';
我也嘗試過:
$resultat2 = $this->db->get_where('membre',array('facebook_id' => $fb_data['uid']));
echo '<pre>';
print_r($resultat2);
echo '</pre>';
但在這兩種情況下,我得到的唯一數組是:
CI_DB_mysql_result Object
(
[conn_id] => Resource id #36
[result_id] => Resource id #61
[result_array] => Array
(
)
[result_object] => Array
(
)
[custom_result_object] => Array
(
)
[current_row] => 0
[num_rows] => 1
[row_data] =>
)
所以[result_id]沒問題,但是沒有數據(據說它應該在[row_data]中打印?)當我只是嘗試使用mysql時,我得到了正確的成員。 但是對於CI,它似乎不起作用。
3-此外,當我嘗試這樣的事情時:
echo $resultat['nom'];
它不被視為一個數組..
所以..是的,我真的不明白..如果有人能夠點燃我?
不要忘記最后使用result()方法,否則你只獲得資源。 像這樣:
$resultat2 = $this->db->get_where('membre',array('facebook_id' => $fb_data['uid']))->result();
這回答了第二個和第三個問題,至於第一個我需要看看你怎么稱呼它 - 代碼看起來沒問題,而我的賭注是你可能以某種方式調用它兩次。
為2和3
這將與你合作
$this->db->select('nom');
$this->db->from('membre');
$query = $this->db->get_where('facebook_id',$fb_data['uid']);
$row= $result->row();
echo '<pre>';
echo'$row->uid'; // that will prent user id , you can change to what ever you want ex. i want tp prent username it will be like this $row->username it should be same as database column
echo '</pre>';
1 - 你確定你沒有兩次調用insert(...)
嗎? 也許您可以使用$this->db->last_query()
來查看結果查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.