How do I fix my query code so that it searches the database using a Date and Time but the Date is a variable from the function? I am new to PHP and SQL and don't know how the Dots ( . ) and Quotations fully work ( "" '' ). How do i fix this piece of code?
WHERE Fixture.date => '$FixtureDate 00:00:00' AND Fixture.date =< '$FixtureDate 23:59:59'
Please See code below
function FSearchDate($FixtureDate) {
try {
$conn = new PDO("mysql:host=" . $GLOBALS['servername'] . ";dbname=DATABASE_NAME", $GLOBALS['username'], $GLOBALS['password']);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$statement = $conn->query("
SELECT Fixtures.fixtureid AS FixtureID,
Fixtures.date AS Date,
Fixtures.week AS Week,
Fixtures.home_team AS HomeTeam,
Fixtures.away_team AS AwayTeam,
FixtureScores.home_team AS HomeScore,
FixtureScores.away_team AS AwayScore
FROM Fixtures
INNER JOIN FixtureScores ON Fixtures.fixtureid=FixtureScores.fixtureid
WHERE Fixture.date => '$FixtureDate 00:00:00' AND Fixture.date =< '$FixtureDate 23:59:59'"
);
$result = $statement->fetch();
if ($result == null) { // Fixture ID Doesn't Exist
echo '<script type="text/javascript">alert("The Fixture ID entered is not valid. Please enter a valid Fixture ID");</script>';
} else {
$GLOBALS['FixtureID'] = $result[0];
$GLOBALS['FixtureDate'] = $result[1];
$GLOBALS['FixtureWeek'] = $result[2];
$GLOBALS['FixtureHomeTeamName'] = $result[3];
$GLOBALS['FixtureAwayTeamName'] = $result[4];
$GLOBALS['FixtureHomeTeamScore'] = $result[5];
$GLOBALS['FixtureAwayTeamScore'] = $result[6];
}
}
catch(PDOException $e) {
echo "An problem occured: " . $e->getMessage();
}
$conn = null;
}
FSearchDate()method, does it work? If so, it's a variable scope.