普通网友 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条)

报告相同问题?

悬赏问题

  • ¥15 电脑开机过商标后就直接这样,求解各位
  • ¥15 mysql , 用自己创建的本地主机和用户名 登录不上
  • ¥15 关于#web项目#的问题,请各位专家解答!
  • ¥15 vmtools环境不正常
  • ¥15 请问如何在Ubuntu系统中安装使用microsoft R open?
  • ¥15 jupyter notebook
  • ¥30 informer时间序列预测
  • ¥20 SSR引物多态性分析
  • ¥15 大漠插件在Win11易语言注册调用和免注册灵异事件,VS上注册调用完全没问题
  • ¥15 Addressable缓存机制做热更新的问题