PHP PDO多个查询没有任何价值 [英] PHP PDO Multiple queries doesnt get any value
问题描述
在此代码之前,我已经进行了内部联接,但是每次我有多个查询时,它似乎都不起作用.但是,当我执行单个查询时,它似乎正常工作.请高手帮帮我.谢谢!
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);
}
?>
推荐答案
尽管可以运行一次执行多个查询通过PDO进行呼叫,这样做的理由不单一.您的情况都不例外.它永远不会像您想的那样工作.不需要像这样在一个调用中填充多个查询,而是需要使用JOIN进行单个查询.
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();
这篇关于PHP PDO多个查询没有任何价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!