?
Solved

Struggling with CodeIgniter $query always returning 1

Posted on 2013-05-26
6
Medium Priority
?
655 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

801 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