Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4547072
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:02:37+00:00 2026-05-21T16:02:37+00:00

I am switching a web-app I am working on to PDO (from the horrible

  • 0

I am switching a web-app I am working on to PDO (from the horrible and unsafe mysql_query) and am having some difficulty with how to adapt an existing query to the new format. The query finds media items of a certain file types:

The GET is formatted like: jpg,png,gif

(The code below was escaping things but has been simplified for this example)

$query   = "SELECT * FROM `media` WHERE `active` = '1' AND `thumb` IS NULL ";
if($_GET['extensions']){
    $extensions = array_filter(explode(',',str_replace(' ','',strtolower($_GET['extensions']))));
    foreach($extensions as $extension){
        $extension_sql[] = "`type` = '$extension' ";
    }
    if(count($extension_sql) > 0){
        $query .= 'AND (' . implode('OR ', $extension_sql) . ')';
    }
}
$query .= "ORDER BY `created` DESC ";
$result = mysql_query($query);
while($media = mysql_fetch_array($result)){     
    // Do stuff
}

Perhaps I was going about this in a totally backwards way and it should be using an IN() function but regardless, I need to convert this into a parameterized PDO statement such as:

$sth = $dbh->prepare("SELECT * FROM `media` WHERE `active` = '1' AND `thumb` IS NULL AND `type` IN(:set) ORDER BY `created` DESC ");
$types = implode(',', array_filter(explode(',',str_replace(' ','',strtolower($_GET['extensions']))));
$sth->bindParam(':set', $types);
$sth->execute();
while($datatype_option_row = $sth->fetch()){
    // Do stuff
}

Clearly this won’t work… but I am trying to be efficient and secure with little success. I might be able to loop over files-types to create the SQL and then loop again over the binds… but wanted to see if anyone here had sage advice for a different approach.

TL;DR: Trying to find the best way to parameterize a dynamic lists of SQL conditionals.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:02:38+00:00Added an answer on May 21, 2026 at 4:02 pm

    Figured it out, the key was the FIND_IN_SET function since it’s a short list of types:

    $type_sql = ($_GET['extensions'])? "AND FIND_IN_SET(`type`, :type)" : "";
    $sth = $dbh->prepare("SELECT * FROM `media` WHERE `active` = '1' AND `thumb` IS NULL $type_sql ORDER BY `created` DESC ");
    $sth->bindParam(':type', $_GET['extensions']);
    $sth->execute();
    while($media = $sth->fetch()){
        // Do stuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're switching from a legacy app written in ColdFusion to an ASP.Net Web Forms
I'm having some trouble regarding a rest request sent from my Windows Phone app
I have a huge Web App that's switching from a HTML-rendered-on-the-server-and-pushed-to-the-client approach to a
I am currently switching from ant to gradle for my multi module web application
After switching from firefox testing to internet explorer testing, some elements couldn't be found
I'm building an iPhone app which gets updates from the web at regular time
My group is thinking about switching our platform for web UI from ASP.net to
I'm switching my web host and backed up my database. Due to some restriction
I am switching to use Maven for my Spring web app projects and I
I'm writing an App that is parsing Data from an XML-Doc on the web.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.