简体   繁体   English

我正在尝试将数组值绑定到 php pdo 中的一个准备好的语句,但只有绑定的数组的第一个元素

[英]i am trying to bind array value a prepared statement in php pdo but only the first element of the array that is bind

I wish to show a time table and below is the code snippet where I suspect the error is.我想显示一个时间表,下面是我怀疑错误所在的代码片段。 The error therein is that the bind is using only the first element of the array I have named class though there are two elements in the class array.其中的错误是绑定仅使用我命名为 class 的数组的第一个元素,尽管 class 数组中有两个元素。 Please can anyone help me figure out where I am going wrong?请谁能帮我弄清楚我哪里出错了?

while($classes = $stmt->fetch(PDO::FETCH_ASSOC)){

       $showtimetable.= "{\"class\":\"";
       $showtimetable.= $classes["Form"].$classes["Label"];
       $showtimetable.= "\",\"subjects\":[";

      //iterate every class in different times to check if they are having a session in the timetable
           foreach($time as $times){

             $selectlessonquery="SELECT `staffnumber`, `subject` FROM `".$timetablename."` WHERE 
            `Form`=? AND `Label`=? AND daytime=?"; 
                 try{ 
                        //this variable contain the array of variable classes
                         echo $classes["Form"].$classes["Label"];
                        //this works fine with output of classes as 1E and IN
                     //on adding these code that will use the value in $classes  only 1E it binding 
                       in prepared statement
                           $stmt=$conn->prepare($selectlessonquery);
                           $stmt->bindParam(1,$classes["Form"]);
                           $stmt->bindParam(2,$classes["Label"]);
                           $stmt->bindParam(3,$times);
                           $stmt->execute();
                           while($subject = $stmt->fetch(PDO::FETCH_ASSOC)){
                               if(!$subject){
                                  //if no matching row then make the value to be empty string
                                   $showtimetable.= "{\"subject\":\"";
                                   $showtimetable.= " \"";
                                   $showtimetable.= " "."},";  
                               }else{
                                   $showtimetable.= "{\"subject\":\"";
                                   $showtimetable.= $subject["subject"]." ";
                                   $showtimetable.= $subject["staffnumber"]."\"},";
                               }  

                           }
                      }catch(Exception $e){

                          echo "error Ocuured ". $e->getMessage();

                      }
                 }

                  $showtimetable=rtrim($showtimetable,",");
                  $showtimetable.="]";
                 $showtimetable.="},";

             }
      echo $showtimetable
 //the output contains on data relevant to 1E and it isnt looping to the other value as expected

You must do this way.你必须这样做。 Because the second argument must be passed in bindParam() by reference.因为第二个参数必须在bindParam()通过引用传递。

$table = 'users';
$sql = "INSERT INTO `{$table}` (username, password, name, age) VALUES(?, ?, ?, ?)";

$stmt = $pdo->prepare($sql);
$stmt->bindParam(1, $username);
$stmt->bindParam(2, $password);
$stmt->bindParam(3, $name);
$stmt->bindParam(4, $age);

$username = 'zill';
$password = '12345';
$name = 'zilanicse';
$age = 31;

// Executes the query
$stmt->execute();

the problem was the positioning of if inside a while causing the code to stop unexpectedly even though it was intended to supply the default value for subject in cases where there was no row fetched.问题是 if 在一段时间内的定位导致代码意外停止,即使它旨在在没有获取行的情况下为主题提供默认值。 the correct way was正确的方法是

      foreach($time as $times){
         $selectlessonquery="SELECT `staffnumber`, `subject` FROM `".$timetablename."` 
          WHERE `Form`=? AND `Label`=? AND daytime=?"; 
              try{ 

                   //this variable contain the array of variable classes
                   $subjects=$conn->prepare($selectlessonquery);
                   $subjects->bindParam(1,$classes["Form"]);
                   $subjects->bindParam(2,$classes["Label"]);
                   $subjects->bindParam(3,$times);
                   $subjects->execute();

                   if($subjects->rowCount()==0){
                       $showtimetable.= "{\"subject\":\"";
                       $showtimetable.= " \"";
                      $showtimetable.= " "."},";  
                  }else{
                    while($subject=$subjects->fetch(PDO::FETCH_ASSOC)){

                         $showtimetable.= "{\"subject\":\"";
                         $showtimetable.= $subject["subject"]." ";
                        $showtimetable.= $subject["staffnumber"]."\"},";
                   }  

               }

          }catch(Exception $e){

                echo "error Ocuured ". $e->getMessage();

          }
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM