2011-11-20 66 views
1

我只是對一個讓我發瘋的查詢有點疑問。 我與CI的工作,和TIS是我第一次使用這個偉大的框架我的CodeIgniter SQL簡單查詢出了什麼問題?

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']; 

它不被視爲一個數組..

所以..是啊,我真的不明白..如果任何人都能照亮我?

回答

4

不要忘記在最後使用result()方法,否則你只能獲取資源。就像這樣:

$resultat2 = $this->db->get_where('membre',array('facebook_id' => $fb_data['uid']))->result(); 

這回答了第二個和第三個問題,作爲第一我需要看你怎麼叫它 - 代碼看起來很好,而且我打賭你可能莫名其妙地叫了兩次。

+0

謝謝你,你是絕對正確的! – DocFunky

0

1 - 您確定不會撥打insert(...)兩次嗎?也許你可以使用$this->db->last_query()來查看結果查詢。

0

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>';