想要将后台PHP获取到数据库显示在前端页面,怎么获取对应的数据?

HTML:

<span id="article_name" style="font-weight: 700;">&nbsp;&nbsp;</span>
<span id="id" style="color: rgb(24, 144, 255); padding-left: 10px;"></span>

active.php:

<?PHP
    header("Content-Type: text/html; charset=utf-8");
    include('conn.php');//链接数据库
    $sql = "select * from article";
    $res = mysqli_query($conn,$sql);
    while ($row = mysqli_fetch_array($res)) {
        echo $row['article_name']."<br>";
        echo $row['id'];
    }
?>

PHP中echo后台都能显示数据,想要将对应数据输出到html前端页面对应的位置,请问该怎么获取?

阅读 5.6k
2 个回答
新手上路,请多包涵

原生的话 将 php 代码 写到 html 文件中,然后使用php执行.
总的来说就是:

  1. 使用php输出html
  2. 使用html嵌入php代码
  3. 使用框架,mvc使用模板变量

如果没有配置php解析器的话就这样写,如果配置了就写成php文件,直接访问 xxx.php 即可

action.html

<?PHP
    header("Content-Type: text/html; charset=utf-8");
    include('conn.php');//链接数据库
    $sql = "select * from article";
    $res = mysqli_query($conn,$sql);
    while ($row = mysqli_fetch_array($res)) {
        echo '<span id="article_name" style="font-weight: 700;"> ' +$row['article_name'] + '</span>';
echo '<span id="id" style="color: rgb(24, 144, 255); padding-left: 10px;">' + echo $row['id']+ '</span>';
    }
?>

你前端ajax回调里边用了json遍历吧,那么后台就必须输出json格式的数据啊,你直接输出普通的字符串,前端收到后怎么遍历?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题