MySQL: Query that filters the results
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`) ";
}
php mysql
|
show 4 more comments
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`) ";
}
php mysql
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 querycategory LIKE ...
php queryproduct_category LIKE
those are 2 different column names :-)
– Alex
Nov 12 at 17:51
Please edit your question to include the output ofecho $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
|
show 4 more comments
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`) ";
}
php mysql
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`) ";
}
php mysql
php mysql
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 querycategory LIKE ...
php queryproduct_category LIKE
those are 2 different column names :-)
– Alex
Nov 12 at 17:51
Please edit your question to include the output ofecho $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
|
show 4 more comments
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 querycategory LIKE ...
php queryproduct_category LIKE
those are 2 different column names :-)
– Alex
Nov 12 at 17:51
Please edit your question to include the output ofecho $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
|
show 4 more comments
1 Answer
1
active
oldest
votes
Seems to be a few things that could cause problems
- You seem to be missing a space between the two parts of the query that you are concatenating
- You are using ` as quotation marks in the latter part of the query
- 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. "') ";
}
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Seems to be a few things that could cause problems
- You seem to be missing a space between the two parts of the query that you are concatenating
- You are using ` as quotation marks in the latter part of the query
- 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. "') ";
}
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
add a comment |
Seems to be a few things that could cause problems
- You seem to be missing a space between the two parts of the query that you are concatenating
- You are using ` as quotation marks in the latter part of the query
- 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. "') ";
}
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
add a comment |
Seems to be a few things that could cause problems
- You seem to be missing a space between the two parts of the query that you are concatenating
- You are using ` as quotation marks in the latter part of the query
- 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. "') ";
}
Seems to be a few things that could cause problems
- You seem to be missing a space between the two parts of the query that you are concatenating
- You are using ` as quotation marks in the latter part of the query
- 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. "') ";
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 queryproduct_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