0

I am tring to perform the following sql query to check if the name that was inserted to the form already created as taxonomy name. But I keep getting this error message:

PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'test' in 'where clause': SELECT COUNT(*) FROM {taxonomy_term_data} WHERE name = test; Array ( ) in get_google_map_api() (line 79 of /Applications/MAMP/htdocs/Drupal/sites/all/modules/toodate/toodate.module).

my function is:

function get_google_map_api($item){
    $city_name = $item;
    $chack_if_is_taxonomy = (db_query("SELECT COUNT(*) FROM {taxonomy_term_data} WHERE name = ".$city_name));       
    var_dump($chack_if_is_taxonomy);
    drupal_exit();
}
6
  • you error does not seem to match the code..? test column is not there in the where clause which u might have entered earlier.
    – harshal
    Sep 13, 2013 at 4:27
  • You are right, I've edited my text
    – Dori
    Sep 13, 2013 at 4:39
  • is the error still occuring ? Pls update the error which correlates to the code.
    – harshal
    Sep 13, 2013 at 4:40
  • Yes the error is still occurring. I did update the error
    – Dori
    Sep 13, 2013 at 4:43
  • What is test ? is it a column or a variable ?
    – harshal
    Sep 13, 2013 at 4:47

2 Answers 2

1

Quick fix would be adding the missing single quotes around your variable. Forgetting the quotes can throw odd errors from this function, such as mixing a string for a column, like in your case.

There are less error prone ways to make short queries like this. See also Molot's answer.

1
  • Thanks, But it didn't show what I wanted to get... I'll post another question about it.
    – Dori
    Sep 13, 2013 at 5:02
1

Don't use db_query() unless you really have to. Build your queries with db_select(). It's harder to make SQL errors using it. Your query would look like this:

// SELECT COUNT(*) FROM {taxonomy_term_data} WHERE name = $city_name
$chack_if_is_taxonomy = db_query('taxonomy_term_data', 'ttd');
$chack_if_is_taxonomy->addField('ttd', 'COUNT(*)', 'count');
$chack_if_is_taxonomy->condition('name', $city_name, '=');

As you can see, no place to make SQL errors, and PHP bugs should be easier to spot in your favourite IDE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.