普通网友 2017-11-08 07:04
浏览 30
已采纳

PHP PDO多个查询不会获得任何值

I already did inner join before this code but then it seems like every time i have multiple queries it doesnt work. But then when I did it single query it seems fine and working. Please help me experts. Thank you!

<?php
include('db.php');
include('function.php');
if(isset($_POST["personal_info_id"]))
{
    $id1 = $_POST["personal_info_id"];
    $id2 = $_POST["personal_info_id"];
    $sql = "SELECT * FROM hr_details WHERE personal_info_id = '$id1'";
    $sql .="SELECT * FROM personal_info WHERE personal_info_id = '$id2'";
    $output = array();  
    $statement = $connection->prepare($sql);
    $statement->execute();
    $result = $statement->fetchAll();
    foreach($result as $row)
    {
        $output["firstname"] = $row["firstname"];
        $output["middlename"] = $row["middlename"];
        $output["lastname"] = $row["lastname"];
        $output["address"] = $row["address"];
        $output["birthdate"] = $row["birthdate"];
        $output["gender"] = $row["gender"];
        $output["religion"] = $row["religion"];
        $output["civil_status"] = $row["civil_status"];
        $output["biometrics"] = $row["biometrics"];
        if($row["image"] != '')
        {
        $output['user_image'] = '<img src="upload/'.$row["image"].'" class="img-thumbnail" width="50" height="35" /><input type="hidden" name="hidden_user_image" value="'.$row["image"].'" />';
        }
        else
        {
          $output['user_image'] = '<input type="hidden" name="hidden_user_image" value="" />';
         }

    }

    echo json_encode($output);

}
?>
  • 写回答

2条回答 默认 最新

  • dqbr37828 2017-11-08 07:18
    关注

    Although it's possible to run multiple queries in one call with PDO, there is not a single reason to do so. Neither your case is an exception. It will never work as you think. Instead of stuffing several queries in one call like this, you need a single query with JOIN.

    Another issue with your code is a cargo cult prepared statement. It looks like a real one but protects nothing. You should use parameters in your prepared query.

    $sql = "SELECT * FROM hr_details hd, personal_info pi WHERE 
            hd.personal_info_id=pi.personal_info_id AND hd.personal_info_id=?";
    $statement = $connection->prepare($sql);
    $statement->execute([$_POST["personal_info_id"]]);
    $result = $statement->fetchAll();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错