简体   繁体   English

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

[英]What's wrong with my CodeIgniter SQL simple Query?

I just have a little question about a query making me crazy. 我只是对一个让我疯狂的查询有一点疑问。 I'm working with CI, and tis is the first time i'm using this great framework 我正在使用CI,这是我第一次使用这个伟大的框架

1— Okay so I have an insert query working rather well : 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);  

And I don't understand why it always insert the data twice ... ! 我不明白为什么它总是插入数据两次......!

2— Then I got a second question : I just wanna found a user with the related facebook_id so i try that : 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>';  

I've also tried : 我也尝试过:

$resultat2 = $this->db->get_where('membre',array('facebook_id' => $fb_data['uid']));
echo '<pre>';
print_r($resultat2);
echo '</pre>';  

But in both case, the only array I got is : 但在这两种情况下,我得到的唯一数组是:

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] =>
)

So the [result_id] is okay, but there is no data (as far as it is supposed to be printed in [row_data] ?) When I simply try on mysql i got the right result with the right member. 所以[result_id]没问题,但是没有数据(据说它应该在[row_data]中打印?)当我只是尝试使用mysql时,我得到了正确的成员。 But with CI, it doesn't seems to work. 但是对于CI,它似乎不起作用。

3— Furthermore, when i Try something like that : 3-此外,当我尝试这样的事情时:

echo $resultat['nom'];  

it isn't considered as an array .. 它不被视为一个数组..

So .. yeah, I don't really understand .. If anyone could enlight me ? 所以..是的,我真的不明白..如果有人能够点燃我?

Don't forget to use the result() method at the end, otherwise you only get resource. 不要忘记最后使用result()方法,否则你只获得资源。 Like this: 像这样:

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

This answers the second and third questions, as for the first I'd need to see how are you calling it - the code looks okay and my bet is you're probably somehow calling it twice. 这回答了第二个和第三个问题,至于第一个我需要看看你怎么称呼它 - 代码看起来没问题,而我的赌注是你可能以某种方式调用它两次。

for 2 and 3 为2和3

that will be work with you 这将与你合作

$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 - are you sure you don't call insert(...) twice? 1 - 你确定你没有两次调用insert(...)吗? Maybe you can use $this->db->last_query() to see the result query. 也许您可以使用$this->db->last_query()来查看结果查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM