Dynamically creating OR conditions by passing an array to a query in MySQL PHP












1















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.










share|improve this question


















  • 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













  • @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
















1















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.










share|improve this question


















  • 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













  • @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














1












1








1








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 20:02









Musayyab NaveedMusayyab Naveed

1318




1318








  • 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













  • @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














  • 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













  • @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








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












2 Answers
2






active

oldest

votes


















2














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.






share|improve this answer
























  • 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



















1














The IN clause will be easier to use than ORs. 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)






share|improve this answer
























  • If this is user data you must use prepared statements with placeholder values.

    – tadman
    Nov 20 '18 at 20:42











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%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









2














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.






share|improve this answer
























  • 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
















2














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.






share|improve this answer
























  • 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














2












2








2







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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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













1














The IN clause will be easier to use than ORs. 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)






share|improve this answer
























  • If this is user data you must use prepared statements with placeholder values.

    – tadman
    Nov 20 '18 at 20:42
















1














The IN clause will be easier to use than ORs. 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)






share|improve this answer
























  • If this is user data you must use prepared statements with placeholder values.

    – tadman
    Nov 20 '18 at 20:42














1












1








1







The IN clause will be easier to use than ORs. 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)






share|improve this answer













The IN clause will be easier to use than ORs. 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)







share|improve this answer












share|improve this answer



share|improve this answer










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



















  • 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


















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.




draft saved


draft discarded














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





















































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

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter