好的。將PHP變量傳遞到mysql_query字符串中有點複雜的問題。在mysql_query字符串中使用PHP變量
$ _GET ['date'];當通過將包含類似:2015_01_07_1
我需要使用$周變量傳遞到表名稱的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);
?>
我知道這是變量拋出錯誤,因爲在sql編輯器中通過phpmyadmin使用查詢,它與2015_01_07_1而不是變量正常工作。 – 2014-10-17 16:27:19
只是刪除「$ week''周圍的單引號,並使用大括號應該是'games_brixx_gastonia _ {$ week} .rank'。在你的情況下不需要大括號,但確保你不會混淆更復雜的變量是一個好習慣。 – bansi 2014-10-17 16:31:09