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 8938265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:35:09+00:00 2026-06-15T10:35:09+00:00

I’ve been batting this one around for a bit, and I cannot seem to

  • 0

I’ve been batting this one around for a bit, and I cannot seem to solve it on my own. I’ve run into known issues/features with PDO and MYSQL that have lead me to the below code.

I’m searching a database for tag IDs that match multiple criteria. I ran into a MYSQL bug that seems to have forced me to use a temporary table to house some of my results before being processed a second time. PDO would not let me reference the temporary table after the first execute (I may have been doing it wrong) or let me prepare multiple statements, so it then lead me to create a stored procedure for this call. After some recent testing I have found yet another issue/feature with PDO that does not allow me to call the stored procedure in the manner that I was looking for.

Here is the SP:

DELIMITER $$
CREATE PROCEDURE sp_searchPrevArticles(IN tagList VARCHAR(255), IN firstArticle INT(10))
BEGIN

CREATE TEMPORARY TABLE at_results (
id INTEGER(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
article_id INTEGER(10) NOT NULL,
datetime DATETIME NOT NULL,
common_tags INTEGER NOT NULL)
SELECT at.article_id, art.datetime, Count(at.article_id) AS common_tags
FROM article_tags AS at 
INNER JOIN articles AS art ON at.article_id = art.article_id
WHERE at.tag_id IN (tagList)
GROUP BY at.article_id
ORDER BY common_tags DESC, art.datetime DESC;

CREATE TEMPORARY TABLE at_article
SELECT id
FROM at_results
WHERE article_id = firstArticle;

SELECT article_id
FROM at_results, at_article
WHERE at_results.id < at_article.id
ORDER BY at_results.id DESC;

END $$
DELIMITER ;

The parameter that isn’t working correctly is tagList. I create tagList with

foreach ($tag_ids as $tag) {
    $tag_list = ($tag_list . $tag . ",");
}
$tag_list = rtrim($tag_list, ',');

Which gives me a dynamic list of tag ids. For this, we can say “4,3,2,1”. When I run this SP without PDO it returns the proper data set. Any time I run this with PDO it only gives me the first tag in the list. In this case it will only return results for “4”.

The PDO call I have is:

$sql = "CALL sp_searchPrevArticles(:tag_list, :first_article)";
$tag_sth = $this->db->prepare($sql);
$tag_sth->execute(array(':tag_list' => $tag_list, ':first_article' => $first_article));
$tag_sth->setFetchMode(PDO::FETCH_ASSOC);
$data = $tag_sth->fetchAll();

As I continue writing them this way I cannot help but think that I am making everything more difficult for myself. I’m still learning and this is the best solution that I have come up with so far. Anyone have an idea on how to get this functioning properly?

Any help is appreciated.

Thanks!

  • 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-06-15T10:35:10+00:00Added an answer on June 15, 2026 at 10:35 am

    PDO quite simply does not handle lists properly unless they are a static length and defined like IN(?,?,?,?). I get around it by:

    <?php
    $list = '1,2,3,4';
    $params = array('a', 'b');
    
    $query = "SELECT * FROM TABLE WHERE thing = ? AND other = ? AND more IN (%s)";
    $query = sprintf($query, $list);
    
    $rs = $dbh->doQuery($query, $params);
    

    It’s kludge-y, but I have yet to see a real solution from PHP regarding lists of dynamic length.

    I just thought of an uglier, but more compliant way to write this:

    <?php
    $list = array(1,2,3,4); //pretend this is dynamic, and always has at least one member
    $params = array('a','b');
    $query = "SELECT * FROM TABLE WHERE thing = ? AND other = ? AND more IN (%s)";
    
    $placeholders = '?';
    for($i=0; $i<count($list); $i++) {
        $placeholders .= ',?';
    }
    $query = sprintf($query, $placeholders);
    $params = array_merge($params, $list);
    
    $rs = $dbh->doQuery($query, $params);
    

    It’s rough and general, but you get the idea. Unless you’re really committed to parameterizing absolutely everything I prefer the first snippet.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I would like to run a str_replace or preg_replace which looks for certain words
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.