如何将双循环查询转换为PDO?

时间:2019-06-17 16:11:49

标签: php mysql pdo

我需要使用PDO代替mysqli。通过一个查询很简单,不幸的是我无法使用双循环进行管理。我将不胜感激。

我已经尝试过类似的方法,但是它不起作用:

try
{
    $sql = "SELECT * FROM table_1";     
    $stmt = $this->conn->prepare($sql);
    $stmt->execute();
    while($row = $stmt->fetch())
    {
        $id_1 = $row[0];
        $name = $row[1];
        print '<div><h2>'.$name.'</h2><ul id="'.$id.'">';
        $sql2 = "SELECT * FROM table_2 WHERE id=$id_1";
        $stmt = $this->conn->prepare($sql2);
        $stmt->execute();    
        while($row2 = $stmt->fetch())
        {
            $id_2 = $row2[0];
            $title = $row2[1];
            print '<li id="'.$id_2.'" ><h3>'.$title.'</h3></li>';
        }
        print '</ul>';
        print '</div>';
    }
    $stmt->closeCursor();
    print '</div>';
}
catch (\PDOException $e) {
    $result['status'] = false;
    $result['message'] = "Error: " . $e->getMessage();
    return $result;
}

原始代码:

$sql = "SELECT * FROM table_1"; 
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
if (mysqli_num_rows($result)){
    while($row = mysqli_fetch_row($result)) { 
        $id_1 = $row[0];
        $name = $row[1];
        print '<div><h2>'.$name.'</h2><ul id="'.$id.'">';
        $sql2 = "SELECT * FROM table_2 WHERE id=$id_1";
        $result2 = mysqli_query($connect, $sql2) or die(mysqli_error($connect));
        if (mysqli_num_rows($result2)){
            while($row2 = mysqli_fetch_row($result2)) { 
                $id_2 = $row2[0];
                $title = $row2[1];
                print '<li id="'.$id_2.'" ><h3>'.$title.'</h3></li>';
            }
        }
        print '</ul>';
        print '</div>';
    }
}

1 个答案:

答案 0 :(得分:1)

您需要为循环内的语句使用其他变量。您还应该使用参数化查询,而不是替换变量。您可以准备一次该语句,然后在循环中执行它。

try
{
    $sql = "SELECT * FROM table_1";     
    $stmt = $this->conn->prepare($sql);
    $stmt->execute();
    $sql2 = "SELECT * FROM table_2 WHERE id = :id";
    $stmt2 = $this->conn->prepare($sql2);
    $stmt2->bindParam(':id', $id_1);
    while($row = $stmt->fetch())
    {
        $id_1 = $row[0];
        $name = $row[1];
        print '<div><h2>'.$name.'</h2><ul id="'.$id.'">';
        $stmt2->execute();   
        while($row2 = $stmt2->fetch())
        {
            $id_2 = $row2[0];
            $title = $row2[1];
            print '<li id="'.$id_2.'" ><h3>'.$title.'</h3></li>';
        }
        print '</ul>';
        print '</div>';
    }
    $stmt->closeCursor();
    print '</div>';
}
catch (\PDOException $e) {
    $result['status'] = false;
    $result['message'] = "Error: " . $e->getMessage();
    return $result;
}

但是,通常最好执行一个将两个表连接起来的查询,而不是循环查询。

try
{
    $sql = "SELECT t1.id AS id_1, t1.name, t2.id2 AS id_2, t2.title
            FROM table_1 AS t1
            LEFT JOIN table_2 AS t2 ON t1.id = t2.id
            ORDER BY id_1";
    $stmt = $this->conn->prepare($sql);
    $stmt->execute();
    $last_id1 = null;
    while($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        $id_1 = $row['id_1'];
        if ($id_1 !== $last_id1) {
            $name = $row['name'];
            if ($last_id1 !== null) {
                print '</ul></div>';
            }
            print '<div><h2>'.$name.'</h2><ul id="'.$id.'">';
        }
        $id_2 = $row2['id_2'];
        $title = $row2['title'];
        if ($id_2 !== null) {
            print '<li id="'.$id_2.'" ><h3>'.$title.'</h3></li>';
        }
    }
    if ($last_id1) {
        print '</ul></div>';
    }
    $stmt->closeCursor();
    print '</div>';
}
catch (\PDOException $e) {
    $result['status'] = false;
    $result['message'] = "Error: " . $e->getMessage();
    return $result;
}