2011-11-20 65 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>';