我的CodeIgniter SQL简单查询有什么问题?


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();

这回答了第二个和第三个问题,至于第一个问题,我需要看看你是如何调用它的——代码看起来不错,我打赌你可能不知怎么调用了两次。

1 -你确定不给insert(...)打两次电话吗?也许您可以使用$this->db->last_query()来查看结果查询。

for 2 and 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>';