• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 699
  • Last Modified:

Struggling with CodeIgniter $query always returning 1

Hi,
I'm running a query using active record in codeigniter and I'm sure there is a simple solution but I can't see it. The function in my model deletes a record. Problem is the part: if ($query->num_rows() > 0) always returns a 1 so the delete occurs. I get why its doing that as its returning a result to me, so with that in mind, I'm getting '1' result back. How can I do this?

// Delete a contest

	function delete($data) {
	
	$message_id = $data['message_id'];
	$account_id = $data['account_id'];

		$this->db->select('cid');
		$this->db->from('messaging');
		$this->db->where('id', $message_id);
		$this->db->where('account_id', $account_id);
		
		$query = $this->db->get();
		
		if ($query->num_rows() > 0) {
			$this->db->delete('messaging', array('id' => $message_id,'account_id' => $account_id));
			$data['campaign'] = $query->row_array();
			} else {
			return 0;
			}
}

Open in new window

0
tjyoung
Asked:
tjyoung
2 Solutions
 
pandlesCommented:
Have you tried line 13:

$query = $this->db->where();
0
 
mcnuteCommented:
count the result array instead:

if (count($query->result() ) > 0 ) {

        // do your stuff here
}

Open in new window

0
 
Ahmed MerghaniCommented:
Your code is correct and I don't think this is your problem. Ensure your user has the sufficient privilege to delete.
But I wounder the thing that at line 17:
$data['campaign'] = $query->row_array();

Open in new window

This is the deleted data, what would you keep it??
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tjyoungAuthor Commented:
Hi,
There are 2 things I'm trying to accomplish. One is I'm deleting a row and the other is I'm passing the $cid back to my controller. I can delete fine, matter of fact, it deletes the row regardless of the "if ($query->num_rows() > 0) {"

I was trying to say: if the $query comes back with more than 'zero' rows, do the delete and pass me back the $cid. If not, then just pass back '0'

Issue was it was doing that step because the $query always returned zero.

I think mcnute's idea is right and plan to try it out once I get in to the office.

Thanks very much.
0
 
Ray PaseurCommented:
PHP is a "loosely typed" language.  This means that variables can be interpreted in different ways, depending on the context of the code.
http://php.net/manual/en/types.comparisons.php
http://php.net/manual/en/language.types.type-juggling.php

When you're not sure what a variable contains, you can use var_dump() -- it's the programmer's best friend!

If you want to return the cid from a results set after SELECT, you might want to capture it after line 13 and store it in a variable for use in the return statement.  Then, if the cid exists, do the DELETE query, else return zero (or FALSE).
0
 
tjyoungAuthor Commented:
Using count was helpful but in the end I took Ray's advice since there was a problem with my logic. I returned a variable and tested the variable instead.

That way I was able to check to make sure I had a value and if so, delete the campaign and if the campaign was successful deleting (in my case its a mailchimp campaign using their API), then go ahead and delete the record of that campaign in my DB.

I was making an assumption previously that one would never exist without the other which somewhere along the way would probably prove to be a bad assumption.

Thanks!
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now