0

I have an integer array containing id values, that I want to search a mixed database by. In pseudocode this would be :

prepare -> Select * From dataset Where id= :NextArrayValue

For all ArrayValues.

I have been googling this question for a while now. I understand how to perform this operation with a single value, or hardcoded as a finite list of values, using bindParam.

I don't want to spam the database with reconnects. I want to return the desired rows by using a single prepared statement that I execute once, and can refer to later. Ideally I would want a function that operates similarly to bindValue, that will repeat the operation for all values of an integer array. Here is a rough draft of what I am trying to do:

    $body_array = array();//how do I cast this to text/string? Should I?
    $wage_array = array();
    $email_array = array();
    $title_array = array();

    $get_posts = $db2->prepare("SELECT * FROM active_posts WHERE id= :post_id");
    $get_posts-> bindValue(':post_id', $posts_array_index); //This is where I have my question
    $get_posts->execute();


    $get_posts->bindColumn("body", $body);
    $get_posts->bindColumn("wage", $wage);
    $get_posts->bindColumn("email", $email);
    $get_posts->bindColumn("title", $title);


    for ($i=0; $i<$posts; $i++ ) {

    $get_posts->fetch(PDO::FETCH_BOUND); //This doesn't actually work yet, but is the method I will use

    array_push($body_array, $body);
    array_push($wage_array, $wage);
    array_push($email_array, $email);
    array_push($title_array, $title);
    }

    print_r($body_array);
    print_r($wage_array);
    print_r($email_array);
    print_r($title_array);
    }
  • Now I know why I couldn't find such a function. It does not exist, as of that old thread. I am using the latest PHP and MYSQL, and I wonder if there have been any updates since? – user3186033 May 10 '14 at 20:23
  • See [Bug #64852](https://bugs.php.net/bug.php?id=64852), which is `Wont fix`. Wez commented: "It would be nice, but unfortunately it can't be done in a sane way at the PDO level; there is no consistent way to express how to handle data binding to array types and in the absence of that, the parameter binding needs to explicitly specify each parameter. This would require a much more powerful SQL parsing layer to resolve and make it work properly, This sort of feature is better implemented in a layer on top of PDO." – eggyal May 10 '14 at 20:32

0 Answers0