Dynamically creating OR conditions by passing an array to a query in MySQL PHP
I am trying to create OR condition dynamically using an array. Given an array, of course names $courses = array('Eng, 'Deu', 'Bio', 'Chemi')
I want to have a SQL query that uses the values of the array in its AND clause with OR conditions like:
SELECT *
FROM classe
/* The OR conditions should be created in AND clause using array */
WHERE class = 'EFG' AND (course = 'Eng' OR course = 'Deu' OR course = 'Bio')
I trying to do it in PHP MySQL.
Any help would be really appreciated.
Thanks in Advance.
php mysql arrays conditional clause
add a comment |
I am trying to create OR condition dynamically using an array. Given an array, of course names $courses = array('Eng, 'Deu', 'Bio', 'Chemi')
I want to have a SQL query that uses the values of the array in its AND clause with OR conditions like:
SELECT *
FROM classe
/* The OR conditions should be created in AND clause using array */
WHERE class = 'EFG' AND (course = 'Eng' OR course = 'Deu' OR course = 'Bio')
I trying to do it in PHP MySQL.
Any help would be really appreciated.
Thanks in Advance.
php mysql arrays conditional clause
1
Maybeand course in( 'Eng', 'Deu', 'Bio')
... should be able to generate that pretty easily withimplode
...or if using PDO this can be much easier.
– user3783243
Nov 20 '18 at 20:04
@user3783243 Be very careful usingimplode
on user data as that's an express train to SQL injection bugs.
– tadman
Nov 20 '18 at 20:42
add a comment |
I am trying to create OR condition dynamically using an array. Given an array, of course names $courses = array('Eng, 'Deu', 'Bio', 'Chemi')
I want to have a SQL query that uses the values of the array in its AND clause with OR conditions like:
SELECT *
FROM classe
/* The OR conditions should be created in AND clause using array */
WHERE class = 'EFG' AND (course = 'Eng' OR course = 'Deu' OR course = 'Bio')
I trying to do it in PHP MySQL.
Any help would be really appreciated.
Thanks in Advance.
php mysql arrays conditional clause
I am trying to create OR condition dynamically using an array. Given an array, of course names $courses = array('Eng, 'Deu', 'Bio', 'Chemi')
I want to have a SQL query that uses the values of the array in its AND clause with OR conditions like:
SELECT *
FROM classe
/* The OR conditions should be created in AND clause using array */
WHERE class = 'EFG' AND (course = 'Eng' OR course = 'Deu' OR course = 'Bio')
I trying to do it in PHP MySQL.
Any help would be really appreciated.
Thanks in Advance.
php mysql arrays conditional clause
php mysql arrays conditional clause
asked Nov 20 '18 at 20:02
Musayyab NaveedMusayyab Naveed
1318
1318
1
Maybeand course in( 'Eng', 'Deu', 'Bio')
... should be able to generate that pretty easily withimplode
...or if using PDO this can be much easier.
– user3783243
Nov 20 '18 at 20:04
@user3783243 Be very careful usingimplode
on user data as that's an express train to SQL injection bugs.
– tadman
Nov 20 '18 at 20:42
add a comment |
1
Maybeand course in( 'Eng', 'Deu', 'Bio')
... should be able to generate that pretty easily withimplode
...or if using PDO this can be much easier.
– user3783243
Nov 20 '18 at 20:04
@user3783243 Be very careful usingimplode
on user data as that's an express train to SQL injection bugs.
– tadman
Nov 20 '18 at 20:42
1
1
Maybe
and course in( 'Eng', 'Deu', 'Bio')
... should be able to generate that pretty easily with implode
...or if using PDO this can be much easier.– user3783243
Nov 20 '18 at 20:04
Maybe
and course in( 'Eng', 'Deu', 'Bio')
... should be able to generate that pretty easily with implode
...or if using PDO this can be much easier.– user3783243
Nov 20 '18 at 20:04
@user3783243 Be very careful using
implode
on user data as that's an express train to SQL injection bugs.– tadman
Nov 20 '18 at 20:42
@user3783243 Be very careful using
implode
on user data as that's an express train to SQL injection bugs.– tadman
Nov 20 '18 at 20:42
add a comment |
2 Answers
2
active
oldest
votes
Instead of so many OR
clauses, you can simply use IN(..)
:
SELECT *
FROM classe
WHERE class = 'EFG' AND course IN ('Eng' ,'Deu', 'Bio')
In the PHP code, you can use implode()
function to convert the array into a comma separated string, and use it in the query string generation.
Thanks for your reply, with implode I am getting a string like this: "Eng , Deu, Bio" and I want the values like this: " 'Eng' ,'Deu', 'Bio' "
– Musayyab Naveed
Nov 20 '18 at 20:30
@MusayyabNaveed your problem statement says that you get an array. Please confirm what exactly are you getting ?
– Madhur Bhaiya
Nov 20 '18 at 20:32
yes it in array: $courses = array('Rel-GK1', 'F6', 'Bio', 'Chemi'); $placeholders = implode(', ', $courses);
– Musayyab Naveed
Nov 20 '18 at 20:33
@MusayyabNaveed check this answer: stackoverflow.com/a/53360802/2469308 You will get the idea on how to do it.
– Madhur Bhaiya
Nov 20 '18 at 20:35
Thank you very much got it :)
– Musayyab Naveed
Nov 20 '18 at 20:41
|
show 1 more comment
The IN
clause will be easier to use than OR
s. If you are using PDO you can take advantage of its execute
binding and build the placeholders dynamically then just pass your array to it.
$courses = array('Eng', 'Deu', 'Bio', 'Chemi');
$placeholders = rtrim(str_repeat('?, ', count($courses)), ', ');
$query = "select * from table WHERE class = 'EFG' AND course in ({$placeholders})";
$stmt = $pdo->prepare($query);
$stmt->execute($courses);
Demo: https://3v4l.org/jcFSv (PDO bit non functional)
If this is user data you must use prepared statements with placeholder values.
– tadman
Nov 20 '18 at 20:42
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%2f53400701%2fdynamically-creating-or-conditions-by-passing-an-array-to-a-query-in-mysql-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Instead of so many OR
clauses, you can simply use IN(..)
:
SELECT *
FROM classe
WHERE class = 'EFG' AND course IN ('Eng' ,'Deu', 'Bio')
In the PHP code, you can use implode()
function to convert the array into a comma separated string, and use it in the query string generation.
Thanks for your reply, with implode I am getting a string like this: "Eng , Deu, Bio" and I want the values like this: " 'Eng' ,'Deu', 'Bio' "
– Musayyab Naveed
Nov 20 '18 at 20:30
@MusayyabNaveed your problem statement says that you get an array. Please confirm what exactly are you getting ?
– Madhur Bhaiya
Nov 20 '18 at 20:32
yes it in array: $courses = array('Rel-GK1', 'F6', 'Bio', 'Chemi'); $placeholders = implode(', ', $courses);
– Musayyab Naveed
Nov 20 '18 at 20:33
@MusayyabNaveed check this answer: stackoverflow.com/a/53360802/2469308 You will get the idea on how to do it.
– Madhur Bhaiya
Nov 20 '18 at 20:35
Thank you very much got it :)
– Musayyab Naveed
Nov 20 '18 at 20:41
|
show 1 more comment
Instead of so many OR
clauses, you can simply use IN(..)
:
SELECT *
FROM classe
WHERE class = 'EFG' AND course IN ('Eng' ,'Deu', 'Bio')
In the PHP code, you can use implode()
function to convert the array into a comma separated string, and use it in the query string generation.
Thanks for your reply, with implode I am getting a string like this: "Eng , Deu, Bio" and I want the values like this: " 'Eng' ,'Deu', 'Bio' "
– Musayyab Naveed
Nov 20 '18 at 20:30
@MusayyabNaveed your problem statement says that you get an array. Please confirm what exactly are you getting ?
– Madhur Bhaiya
Nov 20 '18 at 20:32
yes it in array: $courses = array('Rel-GK1', 'F6', 'Bio', 'Chemi'); $placeholders = implode(', ', $courses);
– Musayyab Naveed
Nov 20 '18 at 20:33
@MusayyabNaveed check this answer: stackoverflow.com/a/53360802/2469308 You will get the idea on how to do it.
– Madhur Bhaiya
Nov 20 '18 at 20:35
Thank you very much got it :)
– Musayyab Naveed
Nov 20 '18 at 20:41
|
show 1 more comment
Instead of so many OR
clauses, you can simply use IN(..)
:
SELECT *
FROM classe
WHERE class = 'EFG' AND course IN ('Eng' ,'Deu', 'Bio')
In the PHP code, you can use implode()
function to convert the array into a comma separated string, and use it in the query string generation.
Instead of so many OR
clauses, you can simply use IN(..)
:
SELECT *
FROM classe
WHERE class = 'EFG' AND course IN ('Eng' ,'Deu', 'Bio')
In the PHP code, you can use implode()
function to convert the array into a comma separated string, and use it in the query string generation.
answered Nov 20 '18 at 20:04


Madhur BhaiyaMadhur Bhaiya
19.6k62236
19.6k62236
Thanks for your reply, with implode I am getting a string like this: "Eng , Deu, Bio" and I want the values like this: " 'Eng' ,'Deu', 'Bio' "
– Musayyab Naveed
Nov 20 '18 at 20:30
@MusayyabNaveed your problem statement says that you get an array. Please confirm what exactly are you getting ?
– Madhur Bhaiya
Nov 20 '18 at 20:32
yes it in array: $courses = array('Rel-GK1', 'F6', 'Bio', 'Chemi'); $placeholders = implode(', ', $courses);
– Musayyab Naveed
Nov 20 '18 at 20:33
@MusayyabNaveed check this answer: stackoverflow.com/a/53360802/2469308 You will get the idea on how to do it.
– Madhur Bhaiya
Nov 20 '18 at 20:35
Thank you very much got it :)
– Musayyab Naveed
Nov 20 '18 at 20:41
|
show 1 more comment
Thanks for your reply, with implode I am getting a string like this: "Eng , Deu, Bio" and I want the values like this: " 'Eng' ,'Deu', 'Bio' "
– Musayyab Naveed
Nov 20 '18 at 20:30
@MusayyabNaveed your problem statement says that you get an array. Please confirm what exactly are you getting ?
– Madhur Bhaiya
Nov 20 '18 at 20:32
yes it in array: $courses = array('Rel-GK1', 'F6', 'Bio', 'Chemi'); $placeholders = implode(', ', $courses);
– Musayyab Naveed
Nov 20 '18 at 20:33
@MusayyabNaveed check this answer: stackoverflow.com/a/53360802/2469308 You will get the idea on how to do it.
– Madhur Bhaiya
Nov 20 '18 at 20:35
Thank you very much got it :)
– Musayyab Naveed
Nov 20 '18 at 20:41
Thanks for your reply, with implode I am getting a string like this: "Eng , Deu, Bio" and I want the values like this: " 'Eng' ,'Deu', 'Bio' "
– Musayyab Naveed
Nov 20 '18 at 20:30
Thanks for your reply, with implode I am getting a string like this: "Eng , Deu, Bio" and I want the values like this: " 'Eng' ,'Deu', 'Bio' "
– Musayyab Naveed
Nov 20 '18 at 20:30
@MusayyabNaveed your problem statement says that you get an array. Please confirm what exactly are you getting ?
– Madhur Bhaiya
Nov 20 '18 at 20:32
@MusayyabNaveed your problem statement says that you get an array. Please confirm what exactly are you getting ?
– Madhur Bhaiya
Nov 20 '18 at 20:32
yes it in array: $courses = array('Rel-GK1', 'F6', 'Bio', 'Chemi'); $placeholders = implode(', ', $courses);
– Musayyab Naveed
Nov 20 '18 at 20:33
yes it in array: $courses = array('Rel-GK1', 'F6', 'Bio', 'Chemi'); $placeholders = implode(', ', $courses);
– Musayyab Naveed
Nov 20 '18 at 20:33
@MusayyabNaveed check this answer: stackoverflow.com/a/53360802/2469308 You will get the idea on how to do it.
– Madhur Bhaiya
Nov 20 '18 at 20:35
@MusayyabNaveed check this answer: stackoverflow.com/a/53360802/2469308 You will get the idea on how to do it.
– Madhur Bhaiya
Nov 20 '18 at 20:35
Thank you very much got it :)
– Musayyab Naveed
Nov 20 '18 at 20:41
Thank you very much got it :)
– Musayyab Naveed
Nov 20 '18 at 20:41
|
show 1 more comment
The IN
clause will be easier to use than OR
s. If you are using PDO you can take advantage of its execute
binding and build the placeholders dynamically then just pass your array to it.
$courses = array('Eng', 'Deu', 'Bio', 'Chemi');
$placeholders = rtrim(str_repeat('?, ', count($courses)), ', ');
$query = "select * from table WHERE class = 'EFG' AND course in ({$placeholders})";
$stmt = $pdo->prepare($query);
$stmt->execute($courses);
Demo: https://3v4l.org/jcFSv (PDO bit non functional)
If this is user data you must use prepared statements with placeholder values.
– tadman
Nov 20 '18 at 20:42
add a comment |
The IN
clause will be easier to use than OR
s. If you are using PDO you can take advantage of its execute
binding and build the placeholders dynamically then just pass your array to it.
$courses = array('Eng', 'Deu', 'Bio', 'Chemi');
$placeholders = rtrim(str_repeat('?, ', count($courses)), ', ');
$query = "select * from table WHERE class = 'EFG' AND course in ({$placeholders})";
$stmt = $pdo->prepare($query);
$stmt->execute($courses);
Demo: https://3v4l.org/jcFSv (PDO bit non functional)
If this is user data you must use prepared statements with placeholder values.
– tadman
Nov 20 '18 at 20:42
add a comment |
The IN
clause will be easier to use than OR
s. If you are using PDO you can take advantage of its execute
binding and build the placeholders dynamically then just pass your array to it.
$courses = array('Eng', 'Deu', 'Bio', 'Chemi');
$placeholders = rtrim(str_repeat('?, ', count($courses)), ', ');
$query = "select * from table WHERE class = 'EFG' AND course in ({$placeholders})";
$stmt = $pdo->prepare($query);
$stmt->execute($courses);
Demo: https://3v4l.org/jcFSv (PDO bit non functional)
The IN
clause will be easier to use than OR
s. If you are using PDO you can take advantage of its execute
binding and build the placeholders dynamically then just pass your array to it.
$courses = array('Eng', 'Deu', 'Bio', 'Chemi');
$placeholders = rtrim(str_repeat('?, ', count($courses)), ', ');
$query = "select * from table WHERE class = 'EFG' AND course in ({$placeholders})";
$stmt = $pdo->prepare($query);
$stmt->execute($courses);
Demo: https://3v4l.org/jcFSv (PDO bit non functional)
answered Nov 20 '18 at 20:10
user3783243user3783243
2,33511121
2,33511121
If this is user data you must use prepared statements with placeholder values.
– tadman
Nov 20 '18 at 20:42
add a comment |
If this is user data you must use prepared statements with placeholder values.
– tadman
Nov 20 '18 at 20:42
If this is user data you must use prepared statements with placeholder values.
– tadman
Nov 20 '18 at 20:42
If this is user data you must use prepared statements with placeholder values.
– tadman
Nov 20 '18 at 20:42
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.
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%2f53400701%2fdynamically-creating-or-conditions-by-passing-an-array-to-a-query-in-mysql-php%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
1
Maybe
and course in( 'Eng', 'Deu', 'Bio')
... should be able to generate that pretty easily withimplode
...or if using PDO this can be much easier.– user3783243
Nov 20 '18 at 20:04
@user3783243 Be very careful using
implode
on user data as that's an express train to SQL injection bugs.– tadman
Nov 20 '18 at 20:42