在mysql_query字符串中使用PHP变量

时间:2021-09-11 00:10:28

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

好男人。在将PHP变量传递到mysql_query字符串时,我遇到了一个有点复杂的问题。

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

$ _GET['日'];传递时将包含如下内容:2015_01_07_1

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

我需要使用$week变量将GET数据传递到表名中。

<?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);
    ?>

3 个解决方案

#1


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

你必须删除字符;它会以games_brixx_gamazing '2015_01_07_1'.rank到达db

#2


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

不管怎样,我还是建议你用陈述代替。检查一下:http://php.net/manual/pt_BR/mysqli.prepare.php

#3


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.

只需删除“字符”。否则,查询将试图从表games_brixx_ga_ '2015_01_ 07_1_2015_ 01_07_ 1中获取数据。

#1


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

你必须删除字符;它会以games_brixx_gamazing '2015_01_07_1'.rank到达db

#2


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

不管怎样,我还是建议你用陈述代替。检查一下:http://php.net/manual/pt_BR/mysqli.prepare.php

#3


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.

只需删除“字符”。否则,查询将试图从表games_brixx_ga_ '2015_01_ 07_1_2015_ 01_07_ 1中获取数据。