0

我正在尝试执行以下操作

$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);

工作并获取表中的所有条目,然后生成 JSON 数组并发送它们,

但是我想查询必须在 JSON 数组中发送所选 id

例如 10、20、30

我假设这可能会在 For 循环中完成

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

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

现在假设我有 id's = 10,20,30 我想将它们全部附加到 JSON 数组中我该怎么做?

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

编辑代码

    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;
    }
4

2 回答 2

1
$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));

顺便说一句,使用原始 API 并不方便。使用数据库抽象库,您的代码可以短至以下 2 行:

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

编辑:
如果你有一个 id 数组,你需要更复杂的代码

$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));

但是在使用数据库抽象库时,会有相同的 2 行:

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

Edit2:
如果你只需要 id

$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));

在使用数据库抽象库时,它仍然是 2 行:

$ids = $db->getCol('SELECT id FROM myTable');
return json_encode(array('Result'=>$ids));
于 2013-01-29T07:33:02.493 回答
0
$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));

基于其他评论:
最佳选择:

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

或者

$sql = 'SELECT id FROM table1';
$sth = $dbh->prepare($sth);
$sth->execute();
$ids = $sth->fetchColumn();
//next look above
于 2013-01-29T07:38:18.663 回答