目前我的页面至少有两个不同的MySQL查询创建的表。在布局方面,我应该按如下方式创建页面。
<?php
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
?>
<html>
<head>
</head>
<body>
<!-- Query 1 and table 1 -->
<?php $query = "SELECT * FROM X;";
try
{
$stmt = $db->prepare($query);
$stmt->execute();
}
}
$rows = $stmt->fetchAll();
?>
<?php foreach($rows as $row): ?>
// Create table 1 from Query
<?php endforeach; ?>
<!-- Query and table 2 -->
<?php $query = "SELECT * FROM Y;";
try
{
$stmt = $db->prepare($query);
$stmt->execute();
}
}
$rows = $stmt->fetchAll();
?>
<?php foreach($rows as $row): ?>
// Create table 2 from Query
<?php endforeach; ?>
</body>
</html>
或者我应该在程序顶部创建$query1
和$query2
并为每个实例创建单独的数组?
如果在会话过程中多次调用数组,还有什么理由将数组存储为_SESSION变量吗?