3

OK guys. I have a somewhat complicated issue with passing PHP variables into the mysql_query string.

The $_GET['date']; when passed will contain something like: 2015_01_07_1

I need to have the GET data passed into the table names using the $week variables.

<?php

    $week= $_GET['date'];

    $con=mysqli_connect("localhost","root","mypassword","beerhandpoker");
    // Check connection
        if (mysqli_connect_errno()) {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

    $result = mysqli_query
    ($con,
        "SELECT games_brixx_gastonia_'$week'.rank, players_brixx_gastonia.name, games_brixx_gastonia_'$week'.points
        FROM games_brixx_gastonia_'$week', players_brixx_gastonia
        WHERE games_brixx_gastonia_'$week'.email = players.email
        ORDER BY games_brixx_gastonia_'$week'.rank
        LIMIT 20"
    );

    echo "<table>
        <tr>
        <th>Rank</th>
        <th>Name</th>
        <th>Points</th>
        </tr>";

    while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['rank'] . "</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['points'] . "</td>";
        echo "</tr>";
    }

    echo "</table>";

    mysqli_close($con);
    ?>
2
  • I know that it is the variable throwing the error because using the query in the sql editor via phpmyadmin, it works fine with 2015_01_07_1 instead of the variable. Commented Oct 17, 2014 at 16:27
  • just remove the single quotes around '$week' and use braces should be games_brixx_gastonia_{$week}.rank. the braces is not needed in your case but it is a good practice to make sure you won't mess with more complex variables. Commented Oct 17, 2014 at 16:31

3 Answers 3

3

Change the string literal to:

"SELECT games_brixx_gastonia_$week.rank,    
players_brixx_gastonia.name,games_brixx_gastonia_$week.points
FROM games_brixx_gastonia_$week, players_brixx_gastonia
WHERE games_brixx_gastonia_$week.email = players_brixx_gastonia.email
ORDER BY games_brixx_gastonia_$week.rank
LIMIT 20"

You have to remove the ' characters; It's going to the db as games_brixx_gastonia_'2015_01_07_1'.rank

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

2 Comments

Fantastic! Thanks so much. I knew it was something simple and I was just overlooking it.
Also I do see the other error I made in the syntax where it shows players.email. It should say players_brixx_gastonia.email
1

Why do you put single quotes? It should work:

SELECT games_brixx_gastonia_{$week}.rank, players_brixx_gastonia.name, games_brixx_gastonia_{$week}.points
FROM games_brixx_gastonia_{$week}, players_brixx_gastonia
WHERE games_brixx_gastonia_{$week}.email = players.email
ORDER BY games_brixx_gastonia_{$week}.rank
LIMIT 20

Anyway, I'd rather advice you to use statement instead. Check it out: http://php.net/manual/pt_BR/mysqli.prepare.php

1 Comment

Yes. I guess it's even useful for code readability and, hopefully, fight SQL-injection.
0

Just remove the ' characters. Otherwise the query would try to get data from the table games_brixx_gastonia_'2015_01_07_1' and not games_brixx_gastonia_2015_01_07_1.

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.