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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:09:17+00:00 2026-06-09T19:09:17+00:00

I have to select some rows from the database using IN operator. I want

  • 0

I have to select some rows from the database using IN operator. I want to do it using prepared statement. This is my code:

<?php
$lastnames = array('braun', 'piorkowski', 'mason', 'nash');
$in_statement = '"' . implode('", "', $lastnames) . '"'; //"braun", "piorkowski", "mason", "nash"

$data_res = $_DB->prepare('SELECT `id`, `name`, `age` FROM `users` WHERE `lastname` IN (?)');
$data_res->bind_param('s', $in_statement);
$data_res->execute();
$result = $data_res->get_result();
while ($data = $result->fetch_array(MYSQLI_ASSOC)) {
    ...
}
?>

But returns nothing although all data exists in the database.

And one more: if i pass $in_statement directly to query and execute it, the data will be returned. So the problem appears on preparing.

I was looking for the question in Google but it wasn’t’ successful. What’s wrong with my code?
Thanks for the help!

  • 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-09T19:09:19+00:00Added an answer on June 9, 2026 at 7:09 pm

    I’ve recently found the solution for my question. Maybe it’s not the best way to do it, but it works nice! Prove me wrong:)

    <?php
    $lastnames = array('braun', 'piorkowski', 'mason', 'nash');
    $arParams = array();
    
    foreach($lastnames as $key => $value) //recreate an array with parameters explicitly passing every parameter by reference
        $arParams[] = &$lastnames[$key];
    
    $count_params = count($arParams);
    
    $int = str_repeat('i',$count_params); //add type for each variable (i,d,s,b); you can also determine type of the variable automatically (is_int, is_float, is_string) in loop, but i don't need it
    array_unshift($arParams,$int); 
    
    $q = array_fill(0,$count_params,'?'); //form string of question marks for statement
    $params = implode(',',$q);
    
    $data_res = $_DB->prepare('SELECT `id`, `name`, `age` FROM `users` WHERE `lastname` IN ('.$params.')');
    call_user_func_array(array($data_res, 'bind_param'), $arParams);
    $data_res->execute();
    $result = $data_res->get_result();
    while ($data = $result->fetch_array(MYSQLI_ASSOC)) {
        ...
    }
    
    $result->free();
    $data_res->close();
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to retrieve some rows from the database using simple SELECT statement
I have some code that grabs some rows from a table and I want
I have some existing code that retrieves data from a database using ADO.NET that
I want to fetch some specific rows from MySQL database using multiple ids, for
I have users table and want to SELECT some rows by bitmask criteria. I'll
so in my database some rows have NULL values, and when I select *
I have a web based program which chooses some records from a database using
I have a table generated from some PHP code that lists a SMALL amount
I want to count some rows in my database and I'm using the following
When I select some data rows from mssql2005 database and bind them to datagrid,

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.