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?

Phil
  • 157,677
  • 23
  • 242
  • 245
Grig Dodon
  • 63
  • 1
  • 7
  • I think that problem, maybe, lies here: http://stackoverflow.com/questions/1314745/php-count-an-stdclass-object Try to return result_array() – sinisake Feb 06 '14 at 23:35

3 Answers3

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.

0
function getUserPoints($email){
return $this->db->where('email', $email)->count_all_results('points');
}
Fabiano Araujo
  • 876
  • 6
  • 19
0
$data = array(  `p_email` => $email);
$query = $this->db->get_where('points',$data);
if($query->num_rows() == 0)
{
   //Write your code
}
else
{
   //your code
}
Reena Shirale
  • 1,992
  • 1
  • 17
  • 15