Php 两条PDO sql语句/为第二条语句使用获取的数据
因此,我是PHP/PDO新手,在将获取的变量传递到第二条语句时遇到了一些问题。我也有例外的问题,我不知道如何为这种情况建立正确的结构Php 两条PDO sql语句/为第二条语句使用获取的数据,php,sql,variables,pdo,Php,Sql,Variables,Pdo,因此,我是PHP/PDO新手,在将获取的变量传递到第二条语句时遇到了一些问题。我也有例外的问题,我不知道如何为这种情况建立正确的结构 try { $connection->beginTransaction(); $stmt = $connection->prepare("CALL sproc_patient_profile_physical_exam_hdr(?,?,?)"); $stmt->bindValue(1,$casenumber_fetch,PDO::PARAM
try {
$connection->beginTransaction();
$stmt = $connection->prepare("CALL sproc_patient_profile_physical_exam_hdr(?,?,?)");
$stmt->bindValue(1,$casenumber_fetch,PDO::PARAM_INT);
$stmt->bindValue(2,$patientid_val,PDO::PARAM_INT);
$stmt->bindValue(3,$enteredby,PDO::PARAM_STR);
while($row = $stmt->fetch()){
echo $physicalexamid_insert=$row['physicalexamid'];
/* I need to use this data for another try and catch or sql statements for example*/
$count_physical_exam_id = count($physical_exam_id);
for ($x=0; $x < $count_physical_exam_id; $x++) {
if (!(empty($physical_exam_id[$x]))) {
try {
$connection->beginTransaction();
$stmt = $connection->prepare("CALL sproc_patient_profile_physical_exam_dtl(?,?,?,?,?,?,?)");
$stmt->bindValue(1,$casenumber_fetch,PDO::PARAM_INT);
$stmt->bindValue(2,1,PDO::PARAM_INT);
$stmt->bindValue(3,$physical_exam_id[$x],PDO::PARAM_INT);
$stmt->bindValue(4,$physical_exam_desc[$x],PDO::PARAM_STR);
$stmt->bindValue(5,$normal[$x],PDO::PARAM_INT);
$stmt->bindValue(6,$undone[$x],PDO::PARAM_INT);
$stmt->bindValue(7,$specific[$x],PDO::PARAM_INT);
$stmt->execute();
$connection->commit();
} catch(PDOException $ex) {
//$connection->rollBack();
echo $ex->getMessage();
}
}
}
}
$stmt->execute();
$connection->commit();
} catch(PDOException $ex) {
$connection->rollBack();
echo $ex->getMessage();
试试{
$connection->beginTransaction();
$stmt=$connection->prepare(“调用存储过程\患者档案\体检\ hdr(?,?)”;
$stmt->bindValue(1,$casenumber_fetch,PDO::PARAM_INT);
$stmt->bindValue(2$patientid_val,PDO::PARAM_INT);
$stmt->bindValue(3,$enteredby,PDO::PARAM_STR);
而($row=$stmt->fetch()){
echo$physicalexamid_insert=$row['physicalexamid'];
/*例如,我需要将这些数据用于另一个try-and-catch或sql语句*/
$count_physical_exam_id=计数($physical_exam_id);
对于($x=0;$x<$count\u physical\u test\u id;$x++){
如果(!(空($physical_-exam_-id[$x])){
试试{
$connection->beginTransaction();
$stmt=$connection->prepare(“调用存储过程\患者档案\体检\ dtl(?,,,,,,,,,,,?)”;
$stmt->bindValue(1,$casenumber_fetch,PDO::PARAM_INT);
$stmt->bindValue(2,1,PDO::PARAM_INT);
$stmt->bindValue(3,$physical_-test_-id[$x],PDO::PARAM_-INT);
$stmt->bindValue(4,$physical_-exam_-desc[$x],PDO::PARAM_-STR);
$stmt->bindValue(5,$normal[$x],PDO::PARAM_INT);
$stmt->bindValue(6,$undone[$x],PDO::PARAM_INT);
$stmt->bindValue(7,$specific[$x],PDO::PARAM_INT);
$stmt->execute();
$connection->commit();
}捕获($ex){
//$connection->rollBack();
echo$ex->getMessage();
}
}
}
}
$stmt->execute();
$connection->commit();
}捕获($ex){
$connection->rollBack();
echo$ex->getMessage();
有人能帮我吗?谢谢。您的代码有几个问题:
$stmt
变量$stmt = $connection->prepare("CALL sproc_patient_profile_physical_exam_hdr(?,?,?)");
$stmt->bindValue(1,$casenumber_fetch,PDO::PARAM_INT);
$stmt->bindValue(2,$patientid_val,PDO::PARAM_INT);
$stmt->bindValue(3,$enteredby,PDO::PARAM_STR);
$stmt->execute();
完成后,您可以像现在这样使用控制语句进行循环(例如,while($row=$stmt->fetch(PDO::fetch_ASSOC)){…}
)
在该控制结构中,不使用变量
$stmt
作为新的(秒)语句,使用类似于$stmt2
或$internal\u stmt
的内容,这样您就不会过度写入外部变量。您遇到了什么问题?您能提一下吗?我有两个语句/过程。它们各自工作(我已经尝试过了)。但我不知道如何同时执行它们。我尝试嵌套try和catch语句,但不起作用。此外,我需要在第二个存储过程中为我的数据插入“$physicalexamid_insert”。谢谢您正在重用同一个变量$stmt
,能否为内部$stmt
使用另一个变量名?