Solved

Struggling with CodeIgniter $query always returning 1

Posted on 2013-05-26
6
569 Views
Last Modified: 2013-05-29
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
Comment
Question by:tjyoung
6 Comments
 

Expert Comment

by:pandles
ID: 39198649
Have you tried line 13:

$query = $this->db->where();
0
 
LVL 11

Assisted Solution

by:mcnute
mcnute earned 250 total points
ID: 39198669
count the result array instead:

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

        // do your stuff here
}

Open in new window

0
 
LVL 8

Expert Comment

by:Ahmed Merghani
ID: 39198888
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:tjyoung
ID: 39198928
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39199072
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
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 39203855
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to dynamically set the form action using jQuery.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now