2012-12-19 38 views
2

我有兩個MySQL數據庫,並且想用PHP變量比較數據。我連接到數據庫,並使用PDO分配變量:比較兩個數組中的多個值PHP

//Database 1 
include_once('client-config.php'); 
try { 
    $conn = new PDO(DB_HOST, DB_USER, DB_PASSWORD, array(PDO::ATTR_PERSISTENT => TRUE)); 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} catch (PDOException $e) { 
    echo 'Connection failed: ' . $e->getMessage(); 
} 

$DB_Name = "pencuy204"; 
$login = $_SESSION['SESS_login']; 
$qry = "SELECT `BetType`, `RiskAmount`, `WinAmount`, `BetDate`, `GameDate`, `BetRotation`, `TeamParticipant`, `MoneyLine`, `Spread`, `OverUnder` 
     FROM `{$login}_bet`"; 
$result = $conn->query($qry); 

// If the SQL query is succesfully performed ($result not false) 
if ($result !== false) { 
// Parse the result set, and adds each row and colums in HTML table 
    foreach ($result as $row) { 
     $BetType[] = $row['BetType']; 
     $BetRiskAmount[] = $row['RiskAmount']; 
     $BetWinAmount[] = $row['WinAmount']; 
     $BetGameDate[] = strtotime($row['GameDate']); 
     $BetTeamParticipant[] = $row['TeamParticipant']; 
     $BetMoneyLine[] = $row['MoneyLine']; 
     $BetSpread[] = $row['Spread']; 
     $BetOverUnder[] = $row['OverUnder']; 
    } 
} 

//Database 2 
try { 
    require_once('bet-config.php'); 
    $conn1 = new PDO(B_DB_HOST, B_DB_USER, B_DB_PASSWORD); 
    $conn1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} catch (PDOException $e) { 
    echo 'Connection failed: ' . $e->getMessage(); 
} 

date_default_timezone_set('CST'); 
$today = date("Y-m-d"); 
$qry = "SELECT `AwayTeam`, `AwayScore`, `HomeTeam`, `HomeScore`, `FeedDate` FROM games"; 
$checkit = $conn1->query($qry); 

if ($checkit !== false) { 
    foreach($checkit as $row1) { 
     $AwayTeam[] = $row1['AwayTeam']; 
     $HomeTeam[] = $row1['HomeTeam']; 
     $AwayScoreData[] = $row1['AwayScore']; 
     $HomeScoreData[] = $row1['HomeScore']; 
     $FeedDate[] = strtotime($row1['FeedDate']); 
    } 
} 

我想要做的是通過數據庫1在某些PHP陣列的每個值運行,比較它們在某些陣列的每個值在數據庫2這裏是循環的例子,我的工作就:

for ($i = 0; $i <= $count; $i++) { 
    foreach ($BetGameDate as $b) { 
     if (($b == $FeedDate[$i])) { 
      foreach ($BetTeamParticipant as $team) { 
       if (($team == $AwayTeam[$i])) { 
        foreach ($BetType as $type) { 
         if (($type == "Money Line")) { 
          if ($AwayScoreData[$i] < $HomeScoreData[$i]) { 
           $BetV[] = "-" . $BetRiskAmount[$i]; 
           $BetC[] = intval('$BetV'); 
          } 

          if ($AwayScoreData[$i] > $HomeScoreData[$i]) { 
           $BetV[] = "+" . $BetWinAmount[$i]; 
           $BetC[] = intval('$BetV'); 
          } 

          if ($AwayScoreData[$i] == $HomeScoreData[$i]) { 
           $BetV[] = 0; 
           $BetC[] = intval('$BetV'); 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

在這個特殊的例子,如果$GameBetDate等於$FeedDate,賭注隊名等於客隊名稱,以及遊戲類型等於到某個字符串,然後根據風險金額計算下注或者贏得數據庫1中該特定賭注(行)的金額。我覺得我對foreach的使用是正確的,但是如何正確使用迭代for循環來對數據庫中的特定值循環訪問數據庫2中的所有值1,如果標準匹配,則使用數據庫1中的值來計算$BetCBetV

+0

不是一個問題。您可能需要將您的主題修改爲一個真正的問題,如「我如何......」?第一。 –

+2

首先你的代碼對我來說太討厭了,我不想讀它。其次是具體的,不要寫所有的代碼,如連接和其他一切。修改你的問題得到更好的答案! – Ali

+0

我同意阿里和我有第二個問題。爲什麼你有兩個數據庫?我想如果你有一個單獨的數據庫並讓數據庫比較值,它可以變得更容易/更快。 – Zombaya

回答

0

我認爲你可以在你的代碼中使用一些小的重構。 要比較值,可以使用array_diff方法。 您從第一個表中的值,(PDO可以返回數組) 選擇第二值,比較...

http://php.net/manual/en/function.array-diff.php