1

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;
}
2
  • try your query without the FSearchDate() method, does it work? If so, it's a variable scope. Commented Nov 17, 2017 at 13:42
  • Please consult a tutorial on PHP & MySQL integration. The "dots" are for string concatenation, and the quotation types (' vs ") matter, especially in the context you're using them. Commented Nov 17, 2017 at 13:45

2 Answers 2

1

Your query is wrong:

WHERE Fixture.date => '$FixtureDate 00:00:00' AND Fixture.date =< '$FixtureDate 23:59:59'"

You're trying to use the => and =< which don't do what you expected.

The => is the separator for associative arrays.

Those should read as >= and <= respectively, as in "more than or equal to" and "less than or equal to".

Note: =< doesn't exist and isn't a valid operator/separator.

Say it out loud: "less than or equal to". We never say "equal to or less than".

Plus, you may have a variable scoping here for the FSearchDate() method, where you would might have to pass the database connection to. Yet, this might not be the case.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use here between

$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 Fixtures.date BETWEEN 'date('Y-m-d H:i:s',$FixtureDate)' AND 'date('Y-m-d H:i:s',$FixtureDate)'"
    );

check this statement,

WHERE Fixtures.date BETWEEN 'date('Y-m-d H:i:s',$FixtureDate)' AND 'date('Y-m-d H:i:s',$FixtureDate)'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.