【问题标题】:What's wrong with my CodeIgniter SQL simple Query?我的 CodeIgniter SQL 简单查询有什么问题?
【发布时间】:2011-11-20 17:38:04
【问题描述】:

我只是有一个小问题,关于一个让我发疯的查询。 我正在使用 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'];  

它不被视为一个数组..

所以..是的,我真的不明白..如果有人能启发我?

【问题讨论】:

    标签: php mysql sql codeigniter


    【解决方案1】:

    最后不要忘记使用 result() 方法,否则只会得到资源。像这样:

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

    这回答了第二个和第三个问题,至于第一个问题,我需要看看你是如何调用它的 - 代码看起来不错,我敢打赌你可能会以某种方式调用它两次。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 2010-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多