简体   繁体   中英

JSON parse Mysql query result in php pdo

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

Eg 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;
    }
$statement = $conn->prepare('SELECT * FROM myTable');
$statement->execute();
$data = array();
while($row = $statement->fetch(PDO::FETCH_ASSOC))) 
{
    $data[$row['id']] = $row;
}
return json_encode(array('Result'=>$data));

Btw, using raw API is not convenient. With Database abstraction library your code can be as short as 2 following lines:

$data = $db->getInd("id",'SELECT * FROM myTable');
return json_encode(array('Result'=>$data));

Edit:
if you have an array of ids, you need more complex code

$ids  = array(1,2,3);
$data = array();
$statement = $conn->prepare('SELECT * FROM myTable WHERE id = :id');
foreach ($ids as $id) {
    $statement->bindValue(':id', $id, PDO::PARAM_STR);
    $statement->execute();
    $data[] = $statement->fetch(PDO::FETCH_OBJ);
}
return json_encode(array('Result'=>$data));

But while using Database abstraction library, there will be the same 2 lines:

$data = $db->getAll('SELECT * FROM myTable where id IN (?a)', $ids);
return json_encode(array('Result'=>$data));

Edit2:
if you need ids only

$statement = $conn->prepare('SELECT id FROM myTable');
$statement->execute();
$data = array();
while($row = $statement->fetch(PDO::FETCH_ASSOC))) 
{
    $data[] = $row['id'];
}
return json_encode(array('Result'=>$data));

while using Database abstraction library, it's still 2 lines:

$ids = $db->getCol('SELECT id FROM myTable');
return json_encode(array('Result'=>$ids));
$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM