MySQL: Query that filters the results












-1














I wish I could make a query that filters the results. It works when I make this request alone:



SELECT * FROM products_details WHERE active = '1' AND category LIKE '% Phobies%';


But in my code, when test, I have this error that appears every time:




Fatal error: Uncaught PDOException: SQLSTATE [42S22]: Column not
found: 1054 Unknown column '' in 'where clause' in C: laragon www
tresorsdufutur ajax.php on line 36




But I do not have an IN in my query and all the terms come from the same column. I really can not find or find my mistake. Here is my code



$ query = "SELECT * FROM products_details WHERE active = '1'";

if (isset ($ category) &&! empty ($ category)) {
$ categorydata = implode ("','", $ category);
$ query. = "AND product_category LIKE (`$categorydata`) ";
}


enter image description here










share|improve this question




















  • 3




    It is weird that you use space character between $ and variable name. Nevertheless, what is the value of $category variable ?
    – Madhur Bhaiya
    Nov 12 at 17:49








  • 4




    Why do you have backticks around $ categorydata ?
    – Madhur Bhaiya
    Nov 12 at 17:49










  • original query category LIKE ... php query product_category LIKE those are 2 different column names :-)
    – Alex
    Nov 12 at 17:51












  • Please edit your question to include the output of echo $query; so we see the query you have built with your code.
    – Progman
    Nov 12 at 17:58






  • 1




    Possible duplicate of When to use single quotes, double quotes, and back ticks in MySQL
    – Progman
    Nov 12 at 18:09
















-1














I wish I could make a query that filters the results. It works when I make this request alone:



SELECT * FROM products_details WHERE active = '1' AND category LIKE '% Phobies%';


But in my code, when test, I have this error that appears every time:




Fatal error: Uncaught PDOException: SQLSTATE [42S22]: Column not
found: 1054 Unknown column '' in 'where clause' in C: laragon www
tresorsdufutur ajax.php on line 36




But I do not have an IN in my query and all the terms come from the same column. I really can not find or find my mistake. Here is my code



$ query = "SELECT * FROM products_details WHERE active = '1'";

if (isset ($ category) &&! empty ($ category)) {
$ categorydata = implode ("','", $ category);
$ query. = "AND product_category LIKE (`$categorydata`) ";
}


enter image description here










share|improve this question




















  • 3




    It is weird that you use space character between $ and variable name. Nevertheless, what is the value of $category variable ?
    – Madhur Bhaiya
    Nov 12 at 17:49








  • 4




    Why do you have backticks around $ categorydata ?
    – Madhur Bhaiya
    Nov 12 at 17:49










  • original query category LIKE ... php query product_category LIKE those are 2 different column names :-)
    – Alex
    Nov 12 at 17:51












  • Please edit your question to include the output of echo $query; so we see the query you have built with your code.
    – Progman
    Nov 12 at 17:58






  • 1




    Possible duplicate of When to use single quotes, double quotes, and back ticks in MySQL
    – Progman
    Nov 12 at 18:09














-1












-1








-1







I wish I could make a query that filters the results. It works when I make this request alone:



SELECT * FROM products_details WHERE active = '1' AND category LIKE '% Phobies%';


But in my code, when test, I have this error that appears every time:




Fatal error: Uncaught PDOException: SQLSTATE [42S22]: Column not
found: 1054 Unknown column '' in 'where clause' in C: laragon www
tresorsdufutur ajax.php on line 36




But I do not have an IN in my query and all the terms come from the same column. I really can not find or find my mistake. Here is my code



$ query = "SELECT * FROM products_details WHERE active = '1'";

if (isset ($ category) &&! empty ($ category)) {
$ categorydata = implode ("','", $ category);
$ query. = "AND product_category LIKE (`$categorydata`) ";
}


enter image description here










share|improve this question















I wish I could make a query that filters the results. It works when I make this request alone:



SELECT * FROM products_details WHERE active = '1' AND category LIKE '% Phobies%';


But in my code, when test, I have this error that appears every time:




Fatal error: Uncaught PDOException: SQLSTATE [42S22]: Column not
found: 1054 Unknown column '' in 'where clause' in C: laragon www
tresorsdufutur ajax.php on line 36




But I do not have an IN in my query and all the terms come from the same column. I really can not find or find my mistake. Here is my code



$ query = "SELECT * FROM products_details WHERE active = '1'";

if (isset ($ category) &&! empty ($ category)) {
$ categorydata = implode ("','", $ category);
$ query. = "AND product_category LIKE (`$categorydata`) ";
}


enter image description here







php mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 18:02

























asked Nov 12 at 17:47









user1987480

4816




4816








  • 3




    It is weird that you use space character between $ and variable name. Nevertheless, what is the value of $category variable ?
    – Madhur Bhaiya
    Nov 12 at 17:49








  • 4




    Why do you have backticks around $ categorydata ?
    – Madhur Bhaiya
    Nov 12 at 17:49










  • original query category LIKE ... php query product_category LIKE those are 2 different column names :-)
    – Alex
    Nov 12 at 17:51












  • Please edit your question to include the output of echo $query; so we see the query you have built with your code.
    – Progman
    Nov 12 at 17:58






  • 1




    Possible duplicate of When to use single quotes, double quotes, and back ticks in MySQL
    – Progman
    Nov 12 at 18:09














  • 3




    It is weird that you use space character between $ and variable name. Nevertheless, what is the value of $category variable ?
    – Madhur Bhaiya
    Nov 12 at 17:49








  • 4




    Why do you have backticks around $ categorydata ?
    – Madhur Bhaiya
    Nov 12 at 17:49










  • original query category LIKE ... php query product_category LIKE those are 2 different column names :-)
    – Alex
    Nov 12 at 17:51












  • Please edit your question to include the output of echo $query; so we see the query you have built with your code.
    – Progman
    Nov 12 at 17:58






  • 1




    Possible duplicate of When to use single quotes, double quotes, and back ticks in MySQL
    – Progman
    Nov 12 at 18:09








3




3




It is weird that you use space character between $ and variable name. Nevertheless, what is the value of $category variable ?
– Madhur Bhaiya
Nov 12 at 17:49






It is weird that you use space character between $ and variable name. Nevertheless, what is the value of $category variable ?
– Madhur Bhaiya
Nov 12 at 17:49






4




4




Why do you have backticks around $ categorydata ?
– Madhur Bhaiya
Nov 12 at 17:49




Why do you have backticks around $ categorydata ?
– Madhur Bhaiya
Nov 12 at 17:49












original query category LIKE ... php query product_category LIKE those are 2 different column names :-)
– Alex
Nov 12 at 17:51






original query category LIKE ... php query product_category LIKE those are 2 different column names :-)
– Alex
Nov 12 at 17:51














Please edit your question to include the output of echo $query; so we see the query you have built with your code.
– Progman
Nov 12 at 17:58




Please edit your question to include the output of echo $query; so we see the query you have built with your code.
– Progman
Nov 12 at 17:58




1




1




Possible duplicate of When to use single quotes, double quotes, and back ticks in MySQL
– Progman
Nov 12 at 18:09




Possible duplicate of When to use single quotes, double quotes, and back ticks in MySQL
– Progman
Nov 12 at 18:09












1 Answer
1






active

oldest

votes


















0














Seems to be a few things that could cause problems




  1. You seem to be missing a space between the two parts of the query that you are concatenating

  2. You are using ` as quotation marks in the latter part of the query

  3. It's been a while since I did PHP but I think you need to concatenate $categorydata


Try this



   $query = "SELECT * FROM products_details WHERE active = '1'";

if (isset($category) && !empty($category)) {
$categorydata = implode ("','", $category);
$query. = " AND product_category IN ('". $categorydata. "') ";
}





share|improve this answer























  • it does not work unfortunately. I always have the same mistake.
    – user1987480
    Nov 12 at 18:43










  • Well, anyway, I don't know what you are doing but it looks like it could be open for SQL injections. Look into using prepared statements or perhaps an ORM.
    – ofurkusi
    Nov 12 at 23:22













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267487%2fmysql-query-that-filters-the-results%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Seems to be a few things that could cause problems




  1. You seem to be missing a space between the two parts of the query that you are concatenating

  2. You are using ` as quotation marks in the latter part of the query

  3. It's been a while since I did PHP but I think you need to concatenate $categorydata


Try this



   $query = "SELECT * FROM products_details WHERE active = '1'";

if (isset($category) && !empty($category)) {
$categorydata = implode ("','", $category);
$query. = " AND product_category IN ('". $categorydata. "') ";
}





share|improve this answer























  • it does not work unfortunately. I always have the same mistake.
    – user1987480
    Nov 12 at 18:43










  • Well, anyway, I don't know what you are doing but it looks like it could be open for SQL injections. Look into using prepared statements or perhaps an ORM.
    – ofurkusi
    Nov 12 at 23:22


















0














Seems to be a few things that could cause problems




  1. You seem to be missing a space between the two parts of the query that you are concatenating

  2. You are using ` as quotation marks in the latter part of the query

  3. It's been a while since I did PHP but I think you need to concatenate $categorydata


Try this



   $query = "SELECT * FROM products_details WHERE active = '1'";

if (isset($category) && !empty($category)) {
$categorydata = implode ("','", $category);
$query. = " AND product_category IN ('". $categorydata. "') ";
}





share|improve this answer























  • it does not work unfortunately. I always have the same mistake.
    – user1987480
    Nov 12 at 18:43










  • Well, anyway, I don't know what you are doing but it looks like it could be open for SQL injections. Look into using prepared statements or perhaps an ORM.
    – ofurkusi
    Nov 12 at 23:22
















0












0








0






Seems to be a few things that could cause problems




  1. You seem to be missing a space between the two parts of the query that you are concatenating

  2. You are using ` as quotation marks in the latter part of the query

  3. It's been a while since I did PHP but I think you need to concatenate $categorydata


Try this



   $query = "SELECT * FROM products_details WHERE active = '1'";

if (isset($category) && !empty($category)) {
$categorydata = implode ("','", $category);
$query. = " AND product_category IN ('". $categorydata. "') ";
}





share|improve this answer














Seems to be a few things that could cause problems




  1. You seem to be missing a space between the two parts of the query that you are concatenating

  2. You are using ` as quotation marks in the latter part of the query

  3. It's been a while since I did PHP but I think you need to concatenate $categorydata


Try this



   $query = "SELECT * FROM products_details WHERE active = '1'";

if (isset($category) && !empty($category)) {
$categorydata = implode ("','", $category);
$query. = " AND product_category IN ('". $categorydata. "') ";
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 18:17

























answered Nov 12 at 18:01









ofurkusi

133




133












  • it does not work unfortunately. I always have the same mistake.
    – user1987480
    Nov 12 at 18:43










  • Well, anyway, I don't know what you are doing but it looks like it could be open for SQL injections. Look into using prepared statements or perhaps an ORM.
    – ofurkusi
    Nov 12 at 23:22




















  • it does not work unfortunately. I always have the same mistake.
    – user1987480
    Nov 12 at 18:43










  • Well, anyway, I don't know what you are doing but it looks like it could be open for SQL injections. Look into using prepared statements or perhaps an ORM.
    – ofurkusi
    Nov 12 at 23:22


















it does not work unfortunately. I always have the same mistake.
– user1987480
Nov 12 at 18:43




it does not work unfortunately. I always have the same mistake.
– user1987480
Nov 12 at 18:43












Well, anyway, I don't know what you are doing but it looks like it could be open for SQL injections. Look into using prepared statements or perhaps an ORM.
– ofurkusi
Nov 12 at 23:22






Well, anyway, I don't know what you are doing but it looks like it could be open for SQL injections. Look into using prepared statements or perhaps an ORM.
– ofurkusi
Nov 12 at 23:22




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267487%2fmysql-query-that-filters-the-results%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python