普通网友 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 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同