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

[eluser]gork7478[/eluser]
I have this problem and I'm trying to figure out what I'm doing wrong. I like to write out my queries, my own personal preference of course. If I do this:
Code:
$this->db->where('key', $key);
$query = $this->db->get('temp_users');
It works fine no problem.
But if I do this
Code:
$sql = 'SELECT * FROM temp_users WHERE key=? LIMIT 1';
$query = $this->db->query($sql, array('key' => $key));
It tells me to check my MySql syntax. Please tell if these two bits of code don't do the same thing.
#2

[eluser]jairoh_[/eluser]
try this one
Code:
$query = $this->db->query($sql, array($key));
#3

[eluser]gork7478[/eluser]
I actually tried that first. And then I tried it that way. Both ways gave the same result.
#4

[eluser]rana[/eluser]
hi gork7478,
You are using a mysql reserved word "key" (http://dev.mysql.com/doc/refman/5.0/en/r...words.html) as column name, an that's why you are having issue. Its recommended to use different column name for your database stability. But you should get it working by using the following query string:

Code:
$sql = 'SELECT * FROM temp_users WHERE [key]=? LIMIT 1';

Hope this helps. Thanks.
#5

[eluser]gork7478[/eluser]
Hi Rana,

Thanks for putting me in the right direction. It didn't work with the square brackets, but I did make it work using backticks. Thanks for the help and for the reference link.
#6

[eluser]rana[/eluser]
Oh, yes, sorry for my mistake, I should have asked you what database you are using, what I showed works for 'sql server' database. Great to hear did able to figure it out yourself. Thanks.




Theme © iAndrew 2016 - Forum software by © MyBB