我想知道是否有一种方法可以用Mysql将HTML从PHP PDO中分离出来,但我真的没有时间使用框架(不是我的网站,只是有点帮助)。所以我把这个代码放在一个文件里。
<?php
error_reporting( ~E_NOTICE );
require_once 'DB/dbconfig.php';
if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
{
$id = $_GET['edit_id'];
$stmt_edit = $DB_con->prepare('SELECT * from person where personId=:uid');
$stmt_edit->execute(array(':uid'=>$id));
$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
extract($edit_row);
}
我可以访问所有的数据,就像:
<?php echo $personName; ?>
问题是,当我试图在另一个文件中使用这个php-stmt代码并将其称为Person::PersonSelect()
时,我无法在HTML文件中使用数据库数据。
我看到了很多案例,每个案例都像echo '<td>$personName</td>';
一样在echo中使用HTML标记,但我不想通过php echo构建HTML。那么,有什么想法吗?感谢
您可以使用include
将PHP代码与HTML代码分离。在HTML代码中,使用<?=$personName?>
访问PHP变量。
在您的情况下:
<?php
error_reporting( ~E_NOTICE );
require_once 'DB/dbconfig.php';
if(isset($_GET['edit_id']) && !empty($_GET['edit_id']))
{
$id = $_GET['edit_id'];
$stmt_edit = $DB_con->prepare('SELECT * from person where personId=:uid');
$stmt_edit->execute(array(':uid'=>$id));
$edit_row = $stmt_edit->fetch(PDO::FETCH_ASSOC);
extract($edit_row);
//include the view here
include('display.php');
}
在文件display.php
中,编写如下内容:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<body>
Hello <?=$personName?>
</body>
</html>