3

我有两个 MySQL 数据库,并想使用 PHP 变量比较数据。我连接到数据库并使用 PDO 分配变量:

//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

4

1 回答 1

0

我认为您可以在代码中使用一些重构。要比较值,您可以使用 array_diff 方法。您从第一个表中选择值,(PDO 可以返回数组)选择第二个值并比较...

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

于 2013-02-23T16:57:18.277 回答