1

Well i have a problem. I should check if an email is in my database. I'm using CI (CodeIgniter)

MODEL:

   function getUsersPoints($email)
{
$query = $this->db->query("SELECT * FROM `points` WHERE `p_email` = '$email'");
return $query->result();
}

CONTROLLER: it comes a function that inserts all the data in table customers, and then i should check if the email is in the second table points, and if not, i should add it. so, what i have done:

$this->model->insert_customer($firstname, $lastname, $email, $telephone, $street, $house);
$getUserPoints = $this->model->getUsersPoints($email);

if(count($getUserPoints) == 0)
    echo "ADD in table points";
else
    echo "DO SOMETHING ELSE";

Can someone tell me what i'm doing wrong?

1

3 Answers 3

1

ActiveRecord's result() returns object, but you want to count() it, so you need to use ->result_array():

function getUsersPoints($email) {
    $query = $this->db->query("SELECT * FROM `points` WHERE `p_email` = '$email'");
    return $query->result_array();
}

You can also use ->count_all_results() in model, it will return just count. But I think, in such case you should use another ActiveRecords methods enstead of query.

Sign up to request clarification or add additional context in comments.

Comments

0
function getUserPoints($email){
return $this->db->where('email', $email)->count_all_results('points');
}

Comments

0
$data = array(  `p_email` => $email);
$query = $this->db->get_where('points',$data);
if($query->num_rows() == 0)
{
   //Write your code
}
else
{
   //your code
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.