使用PDO

时间:2018-04-28 08:15:18

标签: php pdo

我遇到一个问题,即在我的MVC架构中创建了一个重复的数组。我已经完全评论了所有内容并尽可能地检查了代码,但似乎无法找到问题。我怀疑我知道问题出在哪里但不确定如何修复它(请参阅Model()下的代码中的注释)

目录结构

enter image description here

Controller()的一部分

以下是包含login()方法

的控制器的一部分
class Home extends Controller{
 public function login(){
        $this->model('register');
        // I put the below in brackets (create assosiative array) and assign it key users since we would like to access the array for later to loop on and manipulate
        $this->view('home\login', isset($_REQUEST['submitbtn'])? ['users' => $this->model->loginUser('john@gmail.com', 'John')] : false); //hard-coded values for quick testing
        $this->view->render();
    }
}

模型的一部分Register()

class register extends DB
{
    private $db;
    //protected $userInfo = [];

    public function __construct()
    {
        $this->db = DB::getInstance();    
    }
        public function loginUser($email, $pword)
    {
        $sql = "SELECT * FROM users WHERE email = :email AND pword = :pwd";
        $stmnt = $this->db->prepare($sql);
        $stmnt->bindValue(':email', $email);
        $stmnt->bindValue(':pwd', $pword);
        $stmnt->execute();
        if ($stmnt->rowCount() > 0) {
            return  $stmnt->fetchAll(); //I suspect the "duplicate array" problem is here
        }
        return false;
    }
}

观看login.php

的一部分
<form action="" method="post" style="border:1px solid #ccc">
    <div class="container">
        <h1>Sign Up</h1>
        <p>Please fill in this form to create an account.</p>
        <hr>

        <label for="name"><b>Full Name</b></label>
        <input type="text" placeholder="Enter Fullname" name="fullname" id="fullname" required>

        <label for="email"><b>Email</b></label>
        <input type="text" placeholder="email" name="email">
        <label for="pwd"><b>Password</b></label>
        <input type="password" placeholder="Enter Password" name="pwd" id="pwd" required>
        <label>
            <input type="checkbox" checked="checked" name="remember" style="margin-bottom:15px"> Remember me
        </label>

        <p>By creating an account you agree to our <a href="#" style="color:dodgerblue">Terms & Privacy</a>.</p>

        <div class="clearfix">
            <button type="submit" name="submitbtn" class="signupbtn">Sign Up</button>
        </div>
    </div>
</form>
<?php
if(isset($_POST['submitbtn'])){
    if(array_key_exists('users', $this->view_data )){ //$this->view_data comes from View class & contains data passed to view
        echo 'array key exists';
        echo '<pre>';
        print_r($this->view_data);
        echo '</pre>';
        #TODO fix double array problem
        /*foreach ($this->view_data['users'] as  $user){
            echo '<br />';
            echo 'This is the $USER'. $user;
            echo '<br />';
        }*/
    }
}

问题&amp;问题

注意创建的重复数组,我的问题是为什么以及创建的位置?如果我说我怀疑它来自return $stmnt->fetchAll(),那我是对的,如果是这样我该如何解决呢?

Array
(
    [users] => Array
        (
            [0] => Array
                (
                    [userID] => 2
                    [0] => 2
                    [email] => john@gmail.com
                    [1] => john@gmail.com
                    [pword] => John
                    [2] => John
                    [firstname] => John
                    [3] => John
                    [lastname] => Kruger
                    [4] => Kruger
                    [balance] => 
                    [5] => 
                    [phone] => 48484848
                    [6] => 48484848
                    [country] => ZA
                    [7] => ZA
                    [join_date] => 2018-04-24 22:23:36
                    [8] => 2018-04-24 22:23:36
                    [modify_date] => 2018-04-24 22:23:36
                    [9] => 2018-04-24 22:23:36
                    [ip_address] => 127.458.14
                    [10] => 127.458.14
                )

        )

)

更多信息View()课程

class View
{
    protected $view_file; //represents where file is
    protected $view_data; //data you want to pass

    public function __construct($view_file, $view_data)
    {
        $this->view_file = $view_file; //equal to view_file you are receiving
        $this->view_data = $view_data;  //equal to view_data you are receiving
    }

    public function render()
    {
        //if file exists we will go to the view() method which was set in homeController()
        // we are creating the path inside the home inside the view so if file exists only thing we need to do is:
        //include the file
        if (file_exists(VIEW . $this->view_file . '.php')) {
            include VIEW . $this->view_file . '.php';
        }
    }

任何帮助/建议或建设性批评都表示赞赏。请注意,这仍然是一项正在进行中的工作,我也正在学习MVC设计架构并且仍然是一个“菜鸟”。

1 个答案:

答案 0 :(得分:1)

fetchAll()的默认返回是返回一个数组,其中包含由列名和位置索引的元素(PDO :: FETCH_BOTH),如果你只想要名称使用

return  $stmnt->fetchAll(\PDO::FETCH_ASSOC);

返回类型记录在fetch() - http://php.net/manual/en/pdostatement.fetch.php中,其使用与fetchAll()相同的类型。

您可以使用...

为连接的所有语句设置默认模式
$connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);