将MYSQL表数据传递给HTML

时间:2017-05-09 05:01:54

标签: php html mysql

我有一个文件uploadpostinfo.php,

    <html>
<head>
</head>
<body>
<?php
    error_reporting(E_ALL);

    $db = "pickeqco_postinfo";
    $link = mysqli_connect($host, $user, $pass, $db);
    if (!$link) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }

    echo "Success: A proper connection to MySQL was made! The $db database is great." . PHP_EOL;
    echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
    if (!mysqli_select_db($link, $db)) {
        echo "Database not selected";
    }
    $sql = "SELECT id, topic, bullet1, bullet2, expl, source FROM postinformation";

    $res = $list->query($sql);
    if ($res->num_rows > 0) {
        echo "<table><tr><th>ID</th><th>topic</th></tr>";
        // output data of each row
        while($row = $res->fetch_assoc()) {
            echo "<tr><td>".$row["id"]."</td><td>".$row["topic"]." ".$row["bullet1"]."</td></tr>";
        }
        echo "</table>";
    } else {
        echo "0 results";
    }
    //echo nl2br("\n\n$topic\n$bullet1\n$bullet2\n$expl\n$source\n\n");
?>

</body>
</html>

我想从数据库表中获取值并将其插入到我的index.html文件中。 uploadpostinfo.php和index.html位于同一目录中,但是文件不同。

<form action="uploadpostinfo.php" method="post">
    <input name="submit" type="submit" value="getdata"/>
</form>

有人可以帮我把mysql表格数据输入我的html代码吗?

3 个答案:

答案 0 :(得分:0)

index.html中的javascript代码中尝试此操作:

$.get({
    url: 'uploadpostinfo.php',
    dataType:'html',
    success: function(table){
        $('#yourtableid').html(table);
    }
});

答案 1 :(得分:0)

您正在传递结果变量,但数据在$ res中更改

while($row = $result->fetch_assoc()) {

  while($row = $res->fetch_assoc()) {

答案 2 :(得分:0)

尝试此代码将有助于

$.ajax({
        type: "GET",      
        url: uploadpostinfo.php,
        data: dataString,
        complete: function(result){
        $("#showContent").html(result);
    });