Welcome Guest, Not a member yet? Register   Sign In
Active record WHERE clause not working?
#1

[eluser]xarazar[/eluser]
Hi guys,
I'm pretty sure I'm doing something incredibly daft here but after having spent a few hours I still can't find an answer.
I just started with CI2 and the first thing that stopped me and my drags was something I completely didn't expect: I can't force $this->db->where() to work.
The function is laughably simple:
Code:
function get_page($url = 'home'){
    $this->db->where('url', $url);
    $q = $this->db->get('content');
    //$q = $this->db->query('SELECT * FROM content WHERE url = ?', $url);
    if($q->num_rows() == 1) return $q->row(); else return FALSE;
        
}

So... a unique url is passed on and I expect to receive only one result. Nope. The function works as if there was no WHERE clause at all!
The commented line is the one I use right now to actually get the expected result.

So what am I missing? Anybody experienced that?
Many thanks.
#2

[eluser]InsiteFX[/eluser]
Code:
function get_page($url = 'home')
{
    $this->db->where('url', $url);
    $q = $this->db->get('content');

    //$q = $this->db->query('SELECT * FROM content WHERE url = ?', $url);
    if ($q->num_rows() > 0)
    {
        return $q->row();
    }
    else
    {
        return FALSE;
    }
}

InsiteFX
#3

[eluser]guidorossi[/eluser]
You say that you're getting all the rows in the content table and only one should matches the WHERE clause?
#4

[eluser]xarazar[/eluser]
@guidorossi
Correct. Unbelievable but correct...
#5

[eluser]xarazar[/eluser]
@InsiteFX
Thanks buddy.
But I want to make sure that there is only one record with the queried url hence the usage of $q->num_rows() == 1.
I want the function to return false if there is more than one record with the same url.
#6

[eluser]guidorossi[/eluser]
try enabling the profiler with

Code:
$this->output->enable_profiler(TRUE);

at the beginning of the function where you call get_page to see if the query is correct.
#7

[eluser]xarazar[/eluser]
It's getting interesting... The profiler tells me that no queries were run... But the whole page works fine when I use the manual query rather than Active Records.
#8

[eluser]InsiteFX[/eluser]
Code:
function get_page($url = 'home')
{
    $this->db->where('url', $url);
    $this->db->limit(1);

    $q = $this->db->get('content');

    //$q = $this->db->query('SELECT * FROM content WHERE url = ?', $url);
    if ($q->num_rows() > 0)
    {
        return $q->row();
    }
    else
    {
        return FALSE;
    }
}

InsiteFX
#9

[eluser]guidorossi[/eluser]
well, trat's weird...

I'm having the same code like:

Code:
function get_page($url = 'home'){
    $this->db->where('url', $url);
    $q = $this->db->get('content');
    //$q = $this->db->query('SELECT * FROM content WHERE url = ?', $url);
    if($q->num_rows() == 1) return $q->row(); else return FALSE;
        
}

    function getpage(){
        $this->output->enable_profiler(TRUE);
        $this->get_page('http://www.example.com');
    }

and on the profiler I get:

Quote:DATABASE: citest QUERIES: 1
0.0003
SELECT * FROM (`content`) WHERE `url` = 'http://www.example.com'

It seems to be all right...
#10

[eluser]xarazar[/eluser]
Very weird indeed...
I used a very specific routing solution

$route['^(?!admin)\S*'] = "site/index/$1";

But I can't imagine what would this had to do with the WHERE clause not working...
Perhaps it affects the profiler?

Apart from that everything is just a fresh CI setup...




Theme © iAndrew 2016 - Forum software by © MyBB