1

ループの発生と継続などについて少し混乱しています。ユーザーの実際の特権とユーザーの特権を一致させ、新しいものと一致させる2つのSQLクエリがあります。ただし、新しい権限の一部がユーザーの権限と一致する場合は、SQL 挿入をスキップして次の権限に進みたいと考えています。

public static function insertPriveleges($user_id,$priveleges)
{
    $ex = explode(",",$priveleges); // separated by commas
    if(count($ex)>0)
    {
         $x = false;
         foreach($ex as $i => $priv)
         {
             $check_user = mysql_query("SELECT * FROM users_access_codes WHERE user_id='$user_id'") or die(mysql_error()); // get user's current priveleges
             while($check_data = mysql_fetch_array($check_user))
             {
                  if($check_data['access_code']!=$priv)
                  {
                      //if it doesn't match, insert 
                      $sql = "INSERT INTO users_access_codes (uaID,user_id,access_code) VALUES (NULL,'".$user_id."','$priv')";
                  }             
             }
         }
     }
}

ループ内で 2 つ以上のものを一致させる必要がある状況はほとんどありません。そのユーザーに二重の特権を与えないようにする必要があります。内側のループのどこかに 'continue' ステートメントがあるに違いないことはわかっていますが、どこにあるのかわかりません。

4

2 に答える 2

3

INSERTステートメントの後に追加continue 2して、 の先頭に戻ることができますforeach ($ex as ...break;inner の後に何もないため、この場合にも使用できますwhile

ただし、別の方法で行う場合は、実際には必要ありません。特権ごとにテーブルを読み取る代わりに、それらすべてを 1 回読み取って比較します。

このコードは、データベースからすべての権限を取得し、不足している権限のみを挿入し$exます。array_diff()2 つの差を計算するために使用します。

public static function insertPriveleges($user_id, $priveleges)
{
    $ex = explode(",", $priveleges); // separated by commas
    if (count($ex) > 0) {
         // get user's current priveleges
         $check_user = mysql_query("SELECT * FROM users_access_codes 
             WHERE user_id='$user_id'") or die(mysql_error()); 
         $actual = array();
         while ($row = mysql_fetch_array($check_user)) {
             $actual[] = $row['access_code'];
         }

         foreach (array_diff($ex, $actual) as $priv) {
             //if it doesn't match, insert 
             $sql = "INSERT INTO users_access_codes (uaID,user_id,access_code) VALUES (NULL,'".$user_id."','$priv')";
             mysql_query($sql);
         }
     }
}

ところで、INSERT IGNORE INTO競合状態のために使用を検討できますが、ステートメントの戻り値をチェックしていないため、ここでは問題になりません:)

于 2012-12-15T05:34:27.530 に答える
1

INSERTの後にブレークを追加するだけです。

public static function insertPriveleges($user_id,$priveleges)
{
    $ex = explode(",",$priveleges); // separated by commas
    if(count($ex)>0)
    {
         $x = false;
         foreach($ex as $i => $priv)
         {
             $check_user = mysql_query("SELECT * FROM users_access_codes WHERE user_id='$user_id'") or die(mysql_error()); // get user's current priveleges
             while($check_data = mysql_fetch_array($check_user))
             {
                  if($check_data['access_code']!=$priv)
                  {
                      //if it doesn't match, insert 
                      $sql = "INSERT INTO users_access_codes (uaID,user_id,access_code) VALUES (NULL,'".$user_id."','$priv')";
                      break;
                  }             
             }
         }
     }
}

完全を期すために、次のリンクを読むことをお勧めします:http: //php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated

于 2012-12-15T05:28:37.460 に答える