Reputation: 63
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?
Upvotes: 1
Views: 153
Reputation: 2042
$data = array( `p_email` => $email);
$query = $this->db->get_where('points',$data);
if($query->num_rows() == 0)
{
//Write your code
}
else
{
//your code
}
Upvotes: 0
Reputation: 932
function getUserPoints($email){
return $this->db->where('email', $email)->count_all_results('points');
}
Upvotes: 0
Reputation: 479
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.
Upvotes: 1