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

modelSlikeVrijednost code>中我引用了 model code> - 主键。 ModelSlikeVrijednost code>可以包含很多图像(取决于用户)。 我需要删除基于 modelID code>的文件夹。 p>

路径示例: / home / mainSite / public_html / site / img / 1/1 /.

nn

是否可以这样做? p>

代码: p>

  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(); 
 \  n $ stmt1-> bind_result($ slike); 
 
 while($ stmt1-> fetch()){
 if(is_null($ slike)){
 rmdir($ path); 
}  
其他{
 $ slikePath =“/ home / mainSite / public_html / site /".$ slike; 
 if($ slikePath!= $ path){
 unlin  k($ slikePath); 
} 
 rmdir($ path); 
} 
} 
 
 $ stmt1-> close(); 
 
} 
其他{
 printf(“ 预备语句错误:%s 
“,$ mysqli->错误); 
} 
} 
 
 $ stmt-> close(); 
 
} 
  code>   pre> 
 
 

我收到错误 strong>: Prepared Statement错误:命令不同步; 你现在不能运行这个命令准备好的语句错误:命令不同步; 你现在不能运行这个命令 code> p> div>

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);
        }
   }
}

Do not use bare mysqli API.
Get yourself a helper class, like safemysql
Then your code would be

$models = $db->getCol("SELECT modelID FROM model WHERE proizvodacID=?i",$id);
foreach($models as $modelID) {
    $path  = "/home/mainSite/public_html/site/img/$id/$modelID/";
    $sql   = "SELECT modelSlikeVrijednost FROM modelSlike WHERE modelID=?i";
    $sarr  = $db->getCol($sql, $modelID));

    foreach($sarr as $silke) {
        if(!$slike)) {
           rmdir($path);
        } else {
            $slikePath="/home/mainSite/public_html/site/".$slike;
            if($slikePath!=$path){
                unlink($slikePath);
            }
                rmdir($path);
            }
        }
    }
}

But yes, it's better to do it in one query, like prodigitalson said:

$sql = "SELECT ms.modelSlikeVrijednost, m.modelID FROM model m, modelSlike ms
        WHERE ms.modelID= m.modelID AND m.proizvodacID=?i";
$sarr = $db->getCol($sql, $id);
foreach($sarr as $silke) {
    if(!$slike)) {
       rmdir($path);
    } else {
        $slikePath="/home/mainSite/public_html/site/".$slike;
        if($slikePath!=$path){
            unlink($slikePath);
        }
            rmdir($path);
        }
    }
}

The main idea is to get your data already from the query and then use it.