我在mysql数据库中有不同的表,如表1到表5。当我从下拉表列表中选择它时,我想要一个显示每个表的代码。当我尝试执行以下时,我没有得到任何响应,也没有从sql加载数据。
<div>
<form action="table1.php" method="GET">
<input list="name" name="name">
<datalist id="name">
<option value="table 1">
<option value="table 2">
<option value="table 3">
<option value="table 4">
<option value="table 5">
</datalist>
<input type="submit" name="search" value="search">
</form>
</div>
<div>
<table width="600" border="0" cellpadding="1" cellspacing="1">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
</tr>
</table>
在php代码中我为选择值分配一个变量并在程序中给出。但我得到一个错误,该变量未定义或不在$ conn语句中使用。
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sheet";
$tbname= $_GET['name'];
if (isset($_GET['search'])) {
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM '$tbname'");
$stmt->execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
echo "<tr>";
echo "<th>".$result['Column 1']."</th>";
echo "<th>".$result['Column 2']."</th>";
echo "<th>".$result['Column 3']."</th>";
echo "<th>".$result['Column 4']."</th>";
echo "</tr>";
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
}
答案 0 :(得分:3)
您已将表名放在单引号中,这是不正确的,您需要使用反引号。
PHP代码中的以下行是错误的
$stmt = $conn->prepare("SELECT * FROM '$tbname'");
应该改为
$stmt = $conn->prepare("SELECT * FROM `$tbname`");
答案 1 :(得分:1)
您在此处所做的一切都设置了获取模式,您实际上并没有获取任何内容。
变化:
$stmt->setFetchMode(PDO::FETCH_ASSOC);
echo "<tr>";
echo "<th>".$result['Column 1']."</th>";
echo "<th>".$result['Column 2']."</th>";
echo "<th>".$result['Column 3']."</th>";
echo "<th>".$result['Column 4']."</th>";
echo "</tr>";
为:
while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<th>".$result['Column 1']."</th>";
echo "<th>".$result['Column 2']."</th>";
echo "<th>".$result['Column 3']."</th>";
echo "<th>".$result['Column 4']."</th>";
echo "</tr>";
}
答案 2 :(得分:0)
希望这段代码可以帮到你,
我稍微改变了你的代码结构并添加了fetchAll()
方法来从db中检索行,你也可以使用fetch()
方法逐行获取最后我打印这些行最后。
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "sheet";
$tbname= $_GET['name'];
$results = array();
if (isset($_GET['search'])) {
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM `$tbname`");
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$results = $stmt->fetchAll();//Add this line to get db results
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
}
?>
<div>
<form action="table1.php" method="GET">
<input list="name" name="name">
<datalist id="name">
<option value="table 1">
<option value="table 2">
<option value="table 3">
</datalist>
<input type="submit" name="search" value="search">
</form>
</div>
<div>
<table width="600" border="0" cellpadding="1" cellspacing="1">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
</tr>
<?php
if (is_array($results) && count($results)>0) {
foreach ($results as $result) {
echo "<tr>";
echo "<td>".$result['Column 1']."</td>";
echo "<td>".$result['Column 2']."</td>";
echo "<td>".$result['Column 3']."</td>";
echo "<td>".$result['Column 4']."</td>";
echo "</tr>";
}
}
?>
</table>
</div>