Solved

Struggling with CodeIgniter $query always returning 1

Posted on 2013-05-26
6
598 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…
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.

856 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