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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:39:17+00:00 2026-06-01T19:39:17+00:00

I have an array of ids $friends = array(0001, 0002, 0003, 0004) and a

  • 0

I have an array of ids $friends = array(0001, 0002, 0003, 0004) and a database where table_name = friends, column_header = fid. fid in friends may or may not contain one of the friend IDs. I want to input $friends into the query, and return all of the present values that were both in $friends and in a row of fid.

I’m sure the fid={array_values($friends)} is wrong, but I don’t know how to pass the WHERE portion an array of values…

//All DB_X's are defined in another file that is included in this actual file
$db = new PDO("mysql:host=".DB_SERVER.";dbname=".DB_NAME, DB_USER, DB_PASS);
$stmt = $db->prepare("SELECT fid FROM friends WHERE fid={array_values($friends)} ORDER BY fid ASC");
$stmt->execute();

$friendResults = $stmt->fetchAll();
  • 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-01T19:39:19+00:00Added an answer on June 1, 2026 at 7:39 pm

    You will need to make use of SQL’s IN operator:

    SELECT ... FROM ... WHERE foo IN (val1, val2, ...)
    

    You can use PHP’s implode() function to get the desired SQL bit:

    $values = implode(', ', array_values($friends));
    $query = "SELECT ... FROM ... WHERE fid IN ({$values})";
    

    The above will work if your values are numeric. If they are strings, you’ll have to modify the values before implode()ing:

    $values = array_map(array_values($friends), function($value) {
        return "'{$value}'"; // Here is where you could do sanitization
    });
    $values = implode(', ', $values);
    

    PLEASE NOTE: You must properly sanitize the data in $friends to prevent SQL injection.

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

Sidebar

Related Questions

I have an array of ids like 127415157,31323794... (range not known). What is the
I have an array of employee ids: 34 , 35, 40, 67, 54 and
I have an array of products IDs and I need to find the IDs
I have an array of apps with ids and categories like this: [apps] =>
Given an array of ids $galleries = array(1,2,5) I want to have a SQL
I have a set of ids and names in an associative array and in
I have an array of buttons that contain the id's of the buttons. I
I have an array of IDs that are already sorted in descending order as
I have an array of IDs (e.g. array(1, 2, 10, 34, 100, 101) )
I have an array with the ids of x cakes and another associative array

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.