显示结果表SQL

时间:2017-01-15 12:21:29

标签: php html mysql

我正在尝试根据返回的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");
    }
?>

如何在不添加其他表并隐藏“table1”表的情况下使用id “table1”更新HTML表格?我目前正在开始使用PHP。

1 个答案:

答案 0 :(得分:0)

此扩展在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()返回的结果集标识符

<强>与resultType

可选。指定应生成的数组类型。可以是以下值之一:

MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH,