0

複数のステートメントを含む 1 つのクエリを実行して、データベースからレコードを一度に削除したいと考えています。次のようにmysqlクエリを作成します。

$query  = "DELETE FROM myTable1 WHERE indexID = '".$myVar."' LIMIT 1; ";
$query .= "DELETE FROM myTable2 WHERE indexID = '".$myVar."' LIMIT 1; ";
$query .= "DELETE FROM myTable3 WHERE indexID = '".$myVar."' LIMIT 6; ";

次に、クエリを次のように呼び出します。

if(!$result = mysqli_query($db, $query)) { 
   echo "error with $query"; 
} 

私は何か間違ったことをしていますか?phpMyAdmin内でコピーして実行すると、出力されるクエリは問題なく機能します。

4

3 に答える 3

1

クエリの配列を作成してから、次のようにループで実行する必要があると思います

       $query  = array["DELETE FROM myTable1 WHERE indexID = '".$myVar."' LIMIT 1",
         "DELETE FROM myTable2 WHERE indexID = '".$myVar."' LIMIT 1",
          DELETE FROM myTable3 WHERE indexID = '".$myVar."' LIMIT 6"];

そして、あなたはループで実行します

        for($i=0; $i<sizeof($query);$i++)
            {
               $result[$i]=mysql_query($con,$query[$i]);
            }
于 2013-10-02T06:13:02.123 に答える
1

The answer to this question was to use the built-in php function:

mysqli_multi_query($db, $query)

It's not limited to just DELETE queries, but any multi-statement queries. But since DELETE doesn't return any values I am interested in like a SELECT would, we don't need to worry so much about the result set of the query.

In case we are concerned with the results set then array with loop answer provided by kushal could be a helpful starting point.

于 2013-10-02T15:05:39.497 に答える
0

ステートメントを個別に実行する必要があります。mysqli_query() 関数が複数のステートメントをサポートしているとは思いません。

$query[] = "DELETE FROM myTable1 WHERE indexID = '".$myVar."' LIMIT 1; ";
$query[] = "DELETE FROM myTable2 WHERE indexID = '".$myVar."' LIMIT 1; ";
$query[] = "DELETE FROM myTable3 WHERE indexID = '".$myVar."' LIMIT 6; ";

foreach($query as $q) {
    $result = mysqli_query($db, $q);
}
于 2013-10-01T20:48:42.433 に答える