Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Struggling with CodeIgniter $query always returning 1

Posted on 2013-05-26
6
Medium Priority
?
672 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

650 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