我可以将执行的sql查询传递给会话变量吗?

时间:2017-11-21 08:10:04

标签: php session

我想从查询结果中打印出一个表格。我知道我可以在代码的php部分执行此操作,之后我通过回显查询的返回行来运行查询。

但是,我特别想在我的代码的html部分打印出查询结果,因为我想使用我的个人css样式表来设置表的样式。

因此,我决定使用Session变量来保存查询结果并将其传递给我的代码的html部分。然后我假设我可以通过会话变量将结果打印到我自己的表中。

但是,代码的html部分中的会话变量没有从代码的php部分接收任何内容,这让我觉得不可能发送执行的SQL语句。这准确吗?

同样是什么是最佳实践"将查询结果打印到我自己的自定义表格中的方法(理想情况下使用我自己的css样式表)?

这是我的PHP代码:

<?php
session_start();

#errors on for testing
error_reporting(E_ALL);
ini_set('display_errors', 1);

#boolean variable: if Contact Seller variable is set
$ContactSellerClicked = (isset($_POST['contactSeller'])); 

#if Contact Seller Button is Clicked and there is a corresponding userID along with that click
if ($ContactSellerClicked && (isset($_POST['user_id'])) ){



        #assign userID data to variable
        $user_id = $_POST['user_id'];

        #sql query using InnerJoin pulling 
        #firstName, LastName, SchoolName, & Email from student table using userID that matches listing
        $sql = "SELECT DISTINCT `fname`, `lname`, `school_name`, `email` FROM `student` INNER JOIN `listing` ON `student`.`user_id` = `listing`.`user_id` WHERE `student`.`user_id` = '$user_id' ";


        #prepare and execute
        $stmt = $pdo->prepare($sql);
        $stmt->execute();

        $_SESSION['stmt'] = $stmt;
        header("Location: Seller.php");
        return;

        }
?>

以下是相关的HTML代码:

    <table border =1 style=width:500px id="table" class="contact">
 <tr>
            <th>Name</th>
            <th>School Name</th>
            <th>Email</th>
</tr>

<?php

    $stmt = isset($_SESSION['stmt']) ? $_SESSION['stmt'] : null;

    if($stmt != null){
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC) ) {
            echo "<tr><td>";
            echo($row['fname'] . ' ' . $row['lname'] );
            echo("</td><td>");
            echo($row['school_name']);
            echo("</td><td>");
            echo($row['email']);
            echo("</td>");
            echo("</tr>");
        }
    }

    ?>
</table>

提前谢谢。

1 个答案:

答案 0 :(得分:0)

你能吗?是。

你应该吗?绝对不是。

在会话中存储复杂对象或非常深的嵌套多维数组将绝对会破坏您的性能。您的模型也绝对没有业务回应html(这是视图的用途)。

您的控制器应该从模型中检索结果,并将它们传递给视图。您的根本问题是您需要重新考虑您的架构流程。根据你所写的内容,你没有使用类甚至函数,这就是你遇到困难的原因。使用类将允许您维护持久状态,这将允许您按需检索所需的数据。类似的东西:

class Controller {

    //this represents the object that does the query
    private $model;

    //this represents the object that prints the html
    private $view;

    public function __construct() {
        $this->model = new Model();
        $this->view = new View();
    }

    public function getPage() {
        $data = $this->model->getData();
        $this->view->printPage( $data );
    }

那么模型应该是这样的:

class Model {
    public function getData() {
        //assign userID data to variable
        $user_id = $_POST['user_id'];

        //sql query using InnerJoin pulling 
        //firstName, LastName, SchoolName, & Email from student table using userID that matches listing
        $sql = "SELECT DISTINCT `fname`, `lname`, `school_name`, `email` FROM `student` INNER JOIN `listing` ON `student`.`user_id` = `listing`.`user_id` WHERE `student`.`user_id` = '$user_id' ";


        //prepare and execute
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        return $stmt;
    }
}

你的观点应该是这样的:

class View {
    public function printPage( $data ) {
        while ($row = $data->fetch(PDO::FETCH_ASSOC) ) {
            echo "<tr><td>";
            echo($row['fname'] . ' ' . $row['lname'] );
            echo("</td><td>");
            echo($row['school_name']);
            echo("</td><td>");
            echo($row['email']);
            echo("</td>");
            echo("</tr>");
        }
    }

然后你要做的就是:

require_once 'Controller.php';
require_once 'Model.php';
require_once 'View.php';

$controller = new \Controller();
$controller->getPage();

这是非常简化的,您还有许多其他想要考虑的事情。我建议你首先考虑如何properly use objects,然后在花费大量时间之前阅读MVC in general以避免大量的痛苦。如果你不明白模型和视图究竟是什么,你就会碰壁。如果你不使用类或至少使用函数,你很快就会发现无法维护你正在使用的函数。

用最简单的外行人来说:

查看您的计算机,您将看到它有三个基本的可见部分,键盘,塔式/笔记本电脑和屏幕。

  • 键盘是控制器。它做出所有决定(例如:你实际键入的内容),并且不做任何逻辑,并且不打印任何结果。
  • 塔/笔记本电脑(电路板,硬盘驱动器等)完成所有逻辑。它不做任何决策(它只做它被告知的事情)或打印任何结果。
  • 屏幕是视图。它打印所有结果,不做任何决策或逻辑。

关于您可以要求的最简单的摘要。 MVC程序的工作方式完全相同。