将文件PHP转换为PDO

时间:2019-05-05 14:10:25

标签: php mysql pdo

我正在通过添加PDO编辑要登录的PHP文件。我修改了PHP,但无法正常工作。

问题出在$emparray变量中。在没有PDO的文件中,在while循环内,变量包含值“ truer”,而在具有PDO的文件中,变量不包含任何值。我不明白为什么。

有人可以帮我找出我哪里错了吗?

我尝试遵循其他人对Stack Overflow的建议:

  • if (mysqli_num_rows ($ result)> 0)转换为PDO-> link

  • while ($ row = mysqli_fetch_assoc ($ result))转换为PDO-> link

但是它不起作用。

没有PDO的Login.php

<?php

   if($_SERVER['REQUEST_METHOD']=='POST'){
  // echo $_SERVER["DOCUMENT_ROOT"];  // /home1/demonuts/public_html
//including the database connection file
       include_once("config2.php");

    $idAKr = $_POST['idAKr'];
    $cell = $_POST['cellulare'];

     if( $idAKr == '' || $cell == '' ){
            echo json_encode(array( "statusr" => "false","message" => "Inserisci numero di telefono!") );
     }else{
        $query= "SELECT * FROM Ristoratori WHERE cellulare='$cell'";
            $result= mysqli_query($con, $query);

            if(mysqli_num_rows($result) > 0){  
             $query= "SELECT * FROM Ristoratori WHERE cellulare='$cell'";
                         $result= mysqli_query($con, $query);
                     $emparray = array();
                         if(mysqli_num_rows($result) > 0){  
                         while ($row = mysqli_fetch_assoc($result)) {
                                     $emparray[] = $row;
                                   }
                         }
               echo json_encode(array( "statusr" => "truer","message" => "Accesso eseguito", "datar" => $emparray) );
            }else{ 
                echo json_encode(array( "statusr" => "false","message" => "Numero di telefono sbagliato!") );
            }
             mysqli_close($con);
     }
    } else{
            echo json_encode(array( "statusr" => "false","message" => "Errore, riprova!") );
    }
?>

使用PDO的Login.php

<?php

   if($_SERVER['REQUEST_METHOD']=='POST'){
         include_once("config2.php");

    $idAKr = $_POST['idAKr'];
    $cell = $_POST['cellulare'];

     if( $idAKr == '' || $cell == '' ){
            echo json_encode(array( "statusr" => "false","message" => "Inserisci numero di telefono!") );
     }else{

         $conn=$dbh->prepare("SELECT cellulare FROM Ristoratori WHERE cellulare=?");
         $conn->bindParam(1,$cell);
        $result = $conn->execute();

         $rows = $result->fetchAll();
         $num_rows = count($rows);

            if($num_rows > 0){  

             $conn=$dbh->prepare("SELECT cellulare FROM Ristoratori WHERE cellulare=?");
             $conn->bindParam(1,$cell);
            $result = $conn->execute();

                     $emparray = array();

                     $nRows = $pdo->query('SELECT cellulare FROM Ristoratori WHERE cellulare=:cell')->fetchColumn(); 

                         if($nRows > 0){  
                         while ($row = $result->fetch()) {
                                     $emparray[] = $row;
                                   }
                         }
               echo json_encode(array( "statusr" => "truer","message" => "Accesso eseguito", "datar" => $emparray) );
            }else{ 
                echo json_encode(array( "statusr" => "false","message" => "Numero di telefono sbagliato!") );
            }
            $dbh = null;
     }
    } else{
            echo json_encode(array( "statusr" => "false","message" => "Errore, riprova!") );
    }
?>

0 个答案:

没有答案