我想用一个 PHP
变量( $username
)作为 SQL
我正在创建的表。我需要使用这个,因为在我的网页中,每个用户都需要有自己的表,他可以将数据放在其中,当我尝试从表中选择数据时不起作用,我尝试了很多次,但都不起作用,你能帮我解决这个问题吗?
$result = mysqli_query($mysqli, "SELECT * FROM `$username` ORDER BY id DESC");
和
$sql= "SELECT * FROM `$username` ORDER BY data DESC";
这两个都不行,你能帮帮我吗?
这是我的密码
<?php
session_start();
if (!isset($_SESSION['username'])) {
$_SESSION['msg'] = "You must log in first";
header('location: login.php');
}
if (isset($_GET['logout'])) {
session_destroy();
unset($_SESSION['username']);
header("location: login.php");
}
$sql= 'SELECT * FROM '.$username.' ORDER BY data DESC';
?>
<!DOCTYPE html>
<html>
<head></head>
<body>
<br/><br/>
<div>
<table align="center" width='100%' border=0>
<tr bgcolor='#CCCCCC'>
<td>Data</td>
<td>Cantiere</td>
<td>Pranzo</td>
<td>Cena</td>
<td>Hotel</td>
<td>Macchina</td>
<td>Note</td>
<td>Edit/Delete</td>
</tr>
<?php
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['data']."</td>";
echo "<td>".$res['cantiere']."</td>";
echo "<td>".$res['pranzo']."</td>";
echo "<td>".$res['cena']."</td>";
echo "<td>".$res['hotel']."</td>";
echo "<td>".$res['macchina']."</td>";
echo "<td>".$res['note']."</td>";
echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a
href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you
want to delete?')\">Delete</a></td>";
}
?>
</table>
</div>
</body>
</html>
我得到一个错误:
警告:mysqli\u fetch\u array()期望参数1是mysqli\u result,在index.php的第36行中为null
4条答案
按热度按时间z9smfwbn1#
像这样试试
或
使用变量时,需要使用引号
ui7jx7zq2#
尝试添加字符串串联:
和
i2loujxw3#
您需要将变量包含到select语句中,即。
和
xxe27gdn4#
你应该这样做:
因为sql不知道$username是什么
这适用于两个查询
loek建议:
Note that this answer (and the question) contain sql that easily be hijacked! Please prepare your statements before executing them!