dongsha7215 2013-02-20 14:10
浏览 35
已采纳

PHP MySQL在准备好的声明中准备了语​​句

In modelSlikeVrijednost I have a reference to the model-s primary key. ModelSlikeVrijednost can contain a lot of images (depends on the user). I need to delete the folder based on the modelID.

Example of path: /home/mainSite/public_html/site/img/1/1/.

Is it possible to do this ?

Code:

if ($stmt = $mysqli->prepare("SELECT modelID FROM model WHERE proizvodacID='$id'")) {    
    $stmt->execute();

    $stmt->bind_result($modelID);

    while ($stmt->fetch()) {
        $path="/home/mainSite/public_html/site/img/".$id."/".$modelID."/";

        if ($stmt1 = $mysqli->prepare("SELECT modelSlikeVrijednost FROM modelSlike WHERE modelID='$modelID'")) {    
            $stmt1->execute();

            $stmt1->bind_result($slike);

            while ($stmt1->fetch()) {
                if(is_null($slike)){
                    rmdir($path);
                }
                else{
                    $slikePath="/home/mainSite/public_html/site/".$slike;
                    if($slikePath!=$path){
                        unlink($slikePath);
                    }
                   rmdir($path);
                }
             }

            $stmt1->close();

        }
        else {
            printf("Prepared Statement Error: %s
", $mysqli->error);
        }
    }

    $stmt->close();

}

I get this error :Prepared Statement Error: Commands out of sync; you can't run this command now Prepared Statement Error: Commands out of sync; you can't run this command now

  • 写回答

2条回答 默认 最新

  • dongpu2476 2013-02-20 14:12
    关注

    No you cant.... you need to loop through all the results, close the cursor, or use a separate connection.

    However what you are trying to do is better accomplished with a join anyway...

    SELECT ms.modelSlikeVrijednost, m.modelID FROM model m, modelSlike ms
    WHERE ms.modelID= m.modelID
    AND m.proizvodacID ='$id'
    

    This will give you all the information you need in each row.

    However you ar also using prepared statements incorrectly. You shouldnt be passing in php variables directly you should be binding them as parameters to the query:

    $sql = 'SELECT ms.modelSlikeVrijednost, m.modelID FROM model m, modelSlike ms'
           .' WHERE ms.modelID= m.modelID'
           .' AND m.proizvodacID = ?';
    
    if($stmt = $mysqli->prepare($sql)) {
    
       // bind the $id to the parameter as an integer
       $stmt->bind_param('i', $id);
    
       $stmt->execute();
    
       // bind the fields of the result to the same variables you had before
       $stmt->bind_result($slike, $modelID);
    
       // less prone to error if we only type this manually once :-)
       $basePath = "/home/mainSite/public_html/site";
    
       while($stmt->fetch()) {
    
            $path= $basePath . "/img/".$id."/".$modelID."/";
            $slikePath = $basePath . "/" . $slike;
    
            if(is_null($slike)){
              rmdir($path);
            } else {
               if($slikePath!=$path) {
                  unlink($slikePath);
               }
    
               rmdir($path);
            }
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路