1

I have a table with content:
enter image description here

I want to write a direct SQL query to select the time_delivery, where country is, for example, DK in this case.
So it should return 1-5.

I was following a tutorial: https://fishpig.co.uk/magento/tutorials/direct-sql-queries/
And tried modifying query myself like this:

$country= "DK";
$query = 'SELECT time_delivery FROM ' . $table . ' WHERE country = '
     .$country . ' LIMIT 1';

But I get an error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'DK' in 'where clause'

I would assume that it has something to do with the fact the in the tutorial he uses an int but I want to use a string

2
  • Use bind queries. That will solve your problem. It will handle all type conversions. Apr 19, 2017 at 6:42
  • 1
    I think it is because the quotes are wrong, therefore SQL thinks DK corresponds to a column rather than a value "DK". Your full query will look like this: SELECT time_delivery FROM (TABLE) WHERE country = DK LIMIT 1 You can fix this my quoting the value using the database connector. $db->quote($country)
    – Niels
    Apr 19, 2017 at 6:43

2 Answers 2

2

try this way

$country= "DK";
$query = 'SELECT time_delivery FROM ' . $table . ' WHERE country ="'.$country.'" LIMIT 1';
1

add " on value because it takes value as a column name.

   $country= "DK";
    $query = 'SELECT time_delivery FROM ' . $table . ' WHERE country = "'
         .$country . '" LIMIT 1';
1
  • What is mean :-> Column not found: 1054 Unknown column '''' in 'where clause' and my where clause is :-> WHERE ('' = '''') full code : justpaste.it/5vz3d @Qaisar Satti
    – Gem
    Nov 28, 2018 at 12:21

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.