Welcome Guest, Not a member yet? Register   Sign In
Database Query Problem
#1

[eluser]Kyle Wiebers[/eluser]
I'm just getting back into using CodeIgniter for a project and I can't seem to get the method below to work properly. Currently nothing is returned when I try to access it in a Controller. When I try to run the same query in a DBMS it returns the expected value.

Can anyone help?

Code:
function setProject($name) {
        $arr = $this->db->query('SELECT PROJECT_ID FROM PROJECT WHERE TITLE = '.$name.'LIMIT 1')->result_array();
        return $arr[0]['PROJECT_ID'];
    }
#2

[eluser]jedd[/eluser]
[quote author="Kyle Wiebers" date="1256783589"]
Currently nothing is returned when I try to access it in a Controller. When I try to run the same query in a DBMS it returns the expected value.
[/quote]

When you say same query do you mean the absolutely identical same query - and you know it's identical because you have run the profiler, or used the ->db->last_query thing and ascertained that you're using exactly the same code ... or do you mean that you believe it's the same query, despite the fact that it obviously isn't (otherwise it would work, right?)

So .. what did the profiler reveal about what your query got translated to before being zapped off to the DB?

Without wishing to second guess your own debugging attempts, I'd suggest this bit:

Code:
... TITLE = '.$name.'LIMIT 1')

... is asking for trouble. A space before LIMIT would probably make it happier.


EDIT: you should also do some num_row count checks before trying to return any data.
#3

[eluser]Kyle Wiebers[/eluser]
Thanks for the help, it turned out that I just needed to put quotes around the value in the query.
#4

[eluser]jedd[/eluser]
On that note, depending on the history of $name before it lands in this function, you might want to do one of two alternatives:

Code:
$this->db->query('SELECT PROJECT_ID FROM PROJECT WHERE TITLE = '. $this->db->escape($name) .' LIMIT 1');

or

Code:
$sql = 'SELECT PROJECT_ID FROM PROJECT WHERE TITLE = ? LIMIT 1';
$this->db->query($sql, array($name));

These are both explained in the Database class section of the user guide.




Theme © iAndrew 2016 - Forum software by © MyBB