Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将双循环查询转换为PDO?_Php_Mysql_Pdo - Fatal编程技术网

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

Php 如何将双循环查询转换为PDO?,php,mysql,pdo,Php,Mysql,Pdo,我需要使用PDO而不是mysqli。一个查询很简单,但不幸的是,我无法使用双循环进行管理。我将感谢任何帮助 我尝试过类似的方法,但不起作用: try { $sql = "SELECT * FROM table_1"; $stmt = $this->conn->prepare($sql); $stmt->execute(); while($row = $stmt->fetch()) { $id_1 = $ro

我需要使用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=“从表1中选择*”;
$stmt=$this->conn->prepare($sql);
$stmt->execute();
而($row=$stmt->fetch())
{
$id_1=$row[0];
$name=$row[1];
打印“.$name”。
    ”; $sql2=“从表2中选择*,其中id=$id\u 1”; $stmt=$this->conn->prepare($sql2); $stmt->execute(); 而($row2=$stmt->fetch()) { $id_2=$row2[0]; $title=$row2[1]; 打印“
  • .$title.”
  • ”; } 打印“
”; 打印“”; } $stmt->closeCursor(); 打印“”; } 捕获(\p异常$e){ $result['status']=false; $result['message']=“错误:”。$e->getMessage(); 返回$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>';
    }
}
$sql=“从表1中选择*”;
$result=mysqli_query($connect,$sql)或die(mysqli_error($connect));
if(mysqli_num_行($result)){
而($row=mysqli_fetch_row($result)){
$id_1=$row[0];
$name=$row[1];
打印“.$name”。
    ”; $sql2=“从表2中选择*,其中id=$id\u 1”; $result2=mysqli_查询($connect,$sql2)或die(mysqli_错误($connect)); if(mysqli_num_行($result2)){ 而($row2=mysqli_fetch_行($result2)){ $id_2=$row2[0]; $title=$row2[1]; 打印“
  • .$title.”
  • ”; } } 打印“
”; 打印“”; } }
循环内的语句需要使用不同的变量。您还应该使用参数化查询,而不是替换变量。您可以准备此语句一次,然后在循环中执行它

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;
}
试试看
{
$sql=“从表1中选择*”;
$stmt=$this->conn->prepare($sql);
$stmt->execute();
$sql2=“从表2中选择*,其中id=:id”;
$stmt2=$this->conn->prepare($sql2);
$stmt2->bindParam(':id',$id_1);
而($row=$stmt->fetch())
{
$id_1=$row[0];
$name=$row[1];
打印“.$name”。
    ”; $stmt2->execute(); 而($row2=$stmt2->fetch()) { $id_2=$row2[0]; $title=$row2[1]; 打印“
  • .$title.”
  • ”; } 打印“
”; 打印“”; } $stmt->closeCursor(); 打印“”; } 捕获(\p异常$e){ $result['status']=false; $result['message']=“错误:”。$e->getMessage(); 返回$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;
}
试试看
{
$sql=“选择t1.id作为id\u 1,t1.name,t2.id2作为id\u 2,t2.title
从表1中取t1
左联接表_2为t1.id=t2.id上的t2
按id_1订购”;
$stmt=$this->conn->prepare($sql);
$stmt->execute();
$last_id1=null;
而($row=$stmt->fetch(PDO::fetch\U ASSOC))
{
$id_1=$row['id_1'];
如果($id\u 1!=$last\u id1){
$name=$row['name'];
如果($last_id1!==null){
打印“”;
}
打印“.$name”。
    ”; } $id_2=$row2['id_2']; $title=$row2['title']; 如果($id_2!==null){ 打印“
  • .$title.”
  • ”; } } 如果($last_id1){ 打印“
”; } $stmt->closeCursor(); 打印“”; } 捕获(\p异常$e){ $result['status']=false; $result['message']=“错误:”。$e->getMessage(); 返回$result; }
您的代码易受SQL注入攻击。您应该使用预先准备好的语句。首先,您不应该使用双重获取。您应该加入查询。第二个查询需要使用
$stmt2
,而不是重用变量
$stmt
。就像您在原始代码中使用的
$result2