普通网友 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 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型