tags:

views:

21

answers:

3

I keep getting the error 1054 - Unknown column 'apa_calda' in 'where clause' in MySQL. Here is the query:

SELECT user_id FROM `detalii_contor` WHERE tip_contor=apa_calda

I want to use this query in a PHP file but it doesn't give any results. So I tried to write it in the SQL command prompt. Here is what I tried in the PHP file:

$Q = "SELECT id_contor, den_contor FROM detalii_contor WHERE tip_contor='".$contor."'";

$Q = "SELECT id_contor, den_contor FROM detalii_contor WHERE tip_contor='$contor'";

even without "" or without ''.

I wanted to get $contor from a form. I also tried with $_POST['util'] and {$_POST['util']}. I've also tried to set $contor the value I need, but no result.

+1  A: 

mysql meaning that apa_calda is a column name. if it's a value, quote it like

SELECT user_id FROM detalii_contor WHERE tip_contor='apa_calda'

about $_POST['util'] - try to use $_REQUEST - http://php.net/manual/en/reserved.variables.request.php

mitsky
i tried that, same result.
sebastian
sorry, different result, same error:#1054 - Unknown column 'tip_contor' in 'where clause'
sebastian
Unknown column? a you sure? please provide you code, where you get $contor
mitsky
Unknown column 'tip_contor' - this mean that you type wrong column name
mitsky
$contor="apa_calda"; this is how it's definedtip_contor is correct, i checked, rechecked and checked again, no spaces before or after.
sebastian
i added more info below
sebastian
+2  A: 

Field value should be in quotes.
SELECT user_id FROM detalii_contor WHERE tip_contor='apa_calda'

By the way, you should always escape everything that comes from users. For example,

$mysqli = new mysqli("host", "user", "password", "db");
$contor = $mysqli->real_escape_string($_POST['util'] );
$result = $mysqli->query(SELECT id_contor, den_contor FROM detalii_contor WHERE tip_contor='$contor'");

a1ex07
the first line i wrote was in mysql prompt, where i get the error.in php i don't get any results or errors.
sebastian
When you run the first line on mysql prompt, you got an error because you didn't use quotes. mysql_query (or $mysqli->query) returns false in case of error, so you need to check mysql_error() to get error description from the server. From that description you will see what is wrong with your query.
a1ex07
i added more info below
sebastian
A: 

may be you set $contor in function? then you should use directive "global"

mitsky