我有两个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中的值来计算$BetC
和BetV
?
答案 0 :(得分:0)
我认为您可以在代码中使用一点重构。 要比较值,可以使用array_diff方法。 您从第一个表中选择值,(PDO可以返回数组) 选择第二个值并比较......