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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:39:16+00:00 2026-06-17T22:39:16+00:00

I am trying to do following $statement = $conn->prepare(‘SELECT * FROM myTable’); $statement->execute(); if(!($row

  • 0

I am trying to do following

$statement = $conn->prepare('SELECT * FROM myTable');
$statement->execute();

if(!($row = $statement->fetchAll(PDO::FETCH_ASSOC))) 
{
    return false;
}

$conn = null;
} catch(PDOException $e) {
    throw $e;
    return false;
}       

return  return json_encode(array('Result'=>$row);

Works and fetches all entries in a table make then JSON Array and send them,

However I want to make a query where selected ids must be send in a JSON Array

E.g 10, 20, 30

I assume that this will be done in a For loop perhaps

$statement = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
$statement->bindParam(':id', $id, PDO::PARAM_STR);
$statement->execute();

$row = $statement->fetch(PDO::FETCH_OBJ)))

Now suppose i have id’s = 10,20,30 i want to append all of them in a JSON Array How can i do that?

just like return json_encode(array('Result'=>$row);

Edited Code

    function GetMyMembers() 
    {
        $myId = trim($_REQUEST['myId']);

        try {
            $conn = $this->GetDBConnection();

            $statement = $conn->prepare('SELECT valId FROM memList WHERE myId=:myId' );
            $statement->bindParam(':myId', $myId, PDO::PARAM_INT);
            $statement->execute();

            if(!($row = $statement->fetchAll(PDO::FETCH_ASSOC))) 
            {
                return false;
            }

// $row contains ALL THE ID'S

            $placeholders = str_repeat('?,', count($row));
            $placeholders = substr($placeholders, 0, -1);
            $sql = "SELECT id, * FROM players WHERE id IN ($placeholders)";
            $statement = $conn->prepare($sql);
            $statement->execute($row);
            $rows = $sth->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_GROUP);

            $conn = null;
        } catch(PDOException $e) {
            throw $e;
            return false;
        }       
        return $rows;
    }
  • 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-17T22:39:18+00:00Added an answer on June 17, 2026 at 10:39 pm
    $ids = array(1,2,3);
    $placeholders = str_repeat('?,', count($ids));
    $placeholders = substr($placeholders, 0, -1);
    $sql = "SELECT id, * FROM table WHERE id IN ($placeholders)";
    $sth = $dbh->prepare($sql);
    $sth->execute($ids);
    $rows = $sth->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_GROUP);
    echo json_encode(array('Result' => $rows));
    

    Based on additional comments:
    Best option:

    $sql = '
    SELECT *
    FROM table1 AS t1
    INNER JOIN table2 t2
    ON  t2.foreign_key = t1.id
    ';
    

    or

    $sql = 'SELECT id FROM table1';
    $sth = $dbh->prepare($sth);
    $sth->execute();
    $ids = $sth->fetchColumn();
    //next look above
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to do a select statement from a MySql database. The following works when
I am trying to execute the following statement: DELETE a FROM db.table1 a INNER
I am trying some way to optimize following sql statement: exe_sql DELETE FROM tblEvent_type
I currently am trying the following code: $sql = $db->query(select id,username,fullname from userss ORDER
I am trying to remove 6 rows from the database using the following statement
When trying to autowire JdbcUserDetailsManager from Spring Security, I use following statement in appcontext.xml
When I am trying to carry out following statement from Management Studio, it is
I am trying to rewrite the following statement from Coffeescript to Javascript: d.data.name[0..1].toLowerCase() Can
When I'm trying the following statement, a.do(b.do()); suppose I got a NullPointerException from that
I am currently trying to get the following statement to work: SELECT t1.id, t1.row2

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.