我正在尝试根据返回的 SQL 查询显示一个新表。这是用户加载页面时显示的原始表格代码。
表格:
<form action="walkthroughs.php" method="POST">
Platform: <input type="text" name="platform"/><br/>
<input type="submit" value="Search"/>
</form>
<body>
<section class="container">
<div class="row">
<table id="table1" class="table table-bordered">
<thead>
<th>ID</th>
<th width="25%">FAQ Title</th>
<th width="25%">Game</th>
<th width="*">Platform</th>
<th width="15%">Date Created</th>
<th width="15%">Date Modified</th>
</thead>
<tbody>
<?php
mysql_connect("localhost", "root", "password") or die(mysql_error()); //Connect to server
mysql_select_db("walkthroughs") or die("Cannot connect to database"); //connect to database
$query = mysql_query("Select * from faqlist"); // SQL Query
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print '<td align="center">'. $row['FAQ_ID'] . "</td>";
Print '<td align="center">'. $row['FAQ_Title'] . "</td>";
Print '<td align="center">'. $row['Game'] . "</td>";
Print '<td align="center">'. $row['Platforms'] . "</td>";
Print '<td align="center">'. $row['Date_Created'] . "</td>";
Print '<td align="center">'. $row['Date_Modified'] . "</td>";
Print "</tr>";
}
?>
</tbody>
</table>
</div>
</section>
</body>
这是我尝试执行的 PHP 代码。 (我把它放在我的 </html>
标签的底部。)
PHP:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") // Added an if to keep the page secured
{
$platform = mysql_real_escape_string($_POST['platform']);
mysql_connect("localhost", "root", "password") or die(mysql_error()); // Connect to server
mysql_select_db("walkthroughs") or die("Cannot connect to database"); // Connect to database
$query = mysql_query("SELECT FAQ_ID, FAQ_Title, Game, Platforms FROM faqlist WHERE
Platforms LIKE '$platform' OR Platforms LIKE '%$platform' OR Platforms LIKE '$platform%' OR Platforms LIKE '%$platform%'"); // SQL Query
while($row = mysql_fetch_array($query))
{
Print "<tr>";
Print '<td align="center">'. $row['FAQ_ID'] . "</td>";
Print '<td align="center">'. $row['FAQ_Title'] . "</td>";
Print '<td align="center">'. $row['Game'] . "</td>";
Print '<td align="center">'. $row['Platforms'] . "</td>";
Print "</tr>";
}
}
else
{
header("location: walkthroughs.php");
}
?>
如何更新 ID 为“table1”的 HTML 表格,而不添加另一个表格并隐藏“table1”表格?我目前正在开始使用 PHP。
最佳答案
此扩展在 PHP 5.5.0 中已弃用,并在 PHP 7.0.0 中删除。相反,应使用 MySQLi 或 PDO_MySQL 扩展。
我以相同的逻辑分享了一个不同的例子。
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
$result=mysqli_query($con,$sql);
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)\n",$row[0],$row[1]);
// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
printf ("%s (%s)\n",$row["Lastname"],$row["Age"]);
// Free result set
mysqli_free_result($result);
mysqli_close($con);
?>
结果
必填。指定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的结果集标识符
结果类型
可选。指定应生成什么类型的数组。可以是以下值之一:
MYSQLI_ASSOC, MYSQLI_NUM , MYSQLI_BOTH,
关于php - 显示结果表 SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41661022/