Welcome Guest, Not a member yet? Register   Sign In
If row exists with these values
#1

[eluser]Chris Davies[/eluser]
Hi. I have an array with two values:

Code:
$data = array(
    "follow_id" => $follow_id,
    "member_id" => $member_id
);

I am wondering if its possible before inserting these values into the database, can I check to see if a row exists with these values already?

I thought it might be possible with an if else statement, but I am not sure how.

Could anyone show me how I could achieve this?
#2

[eluser]marcogmonteiro[/eluser]
If they exist where? the $follow_id and $member_id?

just do:
Code:
if(isset($follow_id)){
     $data['follow_id'] = $follow_id;
}
if(isset($member_id)){
     $data['member_id'] = $member_id;
}

anyway I'm just guessing on what you really want to do.
#3

[eluser]Chris Davies[/eluser]
Sorry, I wasnt sure how to explain myself.

Here is my full function:

Code:
public function follow_user() {
    
     $follow_id = $this->EE->input->post("follow_id", TRUE);  
     $member_id = $this->EE->session->userdata('member_id');  
      
     $data = array(
      "follow_id" => $follow_id,
      "member_id" => $member_id
     );

     $this->EE->db->insert('follow', $data);  
    
    }

Basically, I want to check in the database that a row with those two values doesn't exist.

I just don't know where to start...
#4

[eluser]LuckyFella73[/eluser]
You have to do a query and get all rows having both
values. If you get a row as a result you don't have
to insert anymore.

Could look like this:
Code:
public function follow_user()
{
  $follow_id = $this->EE->input->post("follow_id", TRUE);
  $member_id = $this->EE->session->userdata('member_id');

  $data = array(
   "follow_id" => $follow_id,
   "member_id" => $member_id
  );

  // check if allready there:
  $this->EE->db->where($data);
  $check = $this->EE->db->get('follow');
  if ($check->num_rows() > 0)
  {
   // entry allready there
   // do something
   return FALSE;
  }
  else
  {
   $this->EE->db->insert('follow', $data);
   return $this->EE->db->insert_id();
  }
    }
#5

[eluser]Chris Davies[/eluser]
Thank you. I see now how I can replicate this.
#6

[eluser]CroNiX[/eluser]
You could probably do it in a single statement
INSERT ... ON DUPLICATE KEY UPDATE ...




Theme © iAndrew 2016 - Forum software by © MyBB