CodeIgniter Forums
SQL Query returns no results (but it's there!) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: SQL Query returns no results (but it's there!) (/showthread.php?tid=55622)



SQL Query returns no results (but it's there!) - El Forum - 11-03-2012

[eluser]jbuk[/eluser]
Hi all,

I'm lost on this one, and I'm sure it's something obvious...

I'm querying a table via CodeIgniter, and it doesn't return any results - but I've checked the output via $this->db->last_query() and run it within my SQL client directly -it returns the record.

Code:
echo $this->db->query("SELECT * FROM listings WHERE  from_email = '$email'")->num_rows();
echo $this->db->last_query();
Returns 0

In PHPMyADMIN
Code:
SELECT * FROM listings WHERE from_email = '[email protected]'

Returns the row

Could it potentially be encoding of the $email variable when CodeIgniter runs it? If I hard code the $email string in the code it works, I've tried wrapping it in utf_encode/utf_decode

I've not had any problems with any other queries in my application at all

Any help appreciated


SQL Query returns no results (but it's there!) - El Forum - 11-03-2012

[eluser]siptik[/eluser]
Hi! What result returns $this->db->last_query();?


SQL Query returns no results (but it's there!) - El Forum - 11-03-2012

[eluser]jbuk[/eluser]
Hi, it's the same as below (which works in Phpmyadmin)

Code:
SELECT * FROM listings WHERE from_email = '[email protected]'

Thanks for the reply


SQL Query returns no results (but it's there!) - El Forum - 11-03-2012

[eluser]siptik[/eluser]
it's works fine for me:
RESULT:

2SELECT * FROM my_files WHERE from_email = '[email protected]'


SQL Query returns no results (but it's there!) - El Forum - 11-03-2012

[eluser]siptik[/eluser]
Send me dump of database ("listings").
and your controller. I'll try to test it.


SQL Query returns no results (but it's there!) - El Forum - 11-03-2012

[eluser]jbuk[/eluser]
Hi

Thanks,

Code:
CREATE TABLE `listings` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `from_email` text CHARACTER SET latin1,
  `details` text CHARACTER SET latin1,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=281 DEFAULT CHARSET=utf8;

Code:
1,[email protected],'testing'

controller code

Code:
$query = $this->db->query("SELECT * FROM listings WHERE from_email = '[email protected]'
   echo $this->db->last_query();



SQL Query returns no results (but it's there!) - El Forum - 11-03-2012

[eluser]siptik[/eluser]
it's strangely!
Your code works fine for me!

Code:
echo $this->db->query("SELECT * FROM listings WHERE  from_email = '[email protected]'")->num_rows();
echo $this->db->last_query();

2 SELECT * FROM listings WHERE from_email = '[email protected]'

I have two rows in database where from_email = '[email protected]'