PHP PDO sqlite3-> fetchAll()返回NULL

时间:2016-11-19 15:58:02

标签: php pdo sqlite

我有以下代码,我无法理解为什么返回NULL。我在浏览器中收到以下输出:

string(5) "admin"

array(3) { ["is_admin"]=> NULL ["username"]=> NULL ["password"]=> NULL }

ORM.php

<?php
    class ORM
    {
        private $pdo;
        private $query;

        public function __construct() {
            try {
                if ($this->pdo == NULL) {
                    $this->pdo = new \PDO('sqlite:' . SQLITE3_FILE);
                }
            } catch(\PDOException $e) {
                die($e);
            }

            return $this->pdo;
        }

        public function read($query) {
            try {
                $this->query = $this->pdo->query($query);

                $users = [];

                while ($record = $this->query->fetchAll(\PDO::FETCH_ASSOC)) {
                    $users = [
                        'is_admin' => $record['is_admin'],
                        'username' => $record['username'],
                        'password' => $record['password']
                    ];
                }

                return $users;
            } catch(\PDOException $e) {
                die($e);
            }
        }
    }

的index.php

<?php
    require_once('ORM.php');

    $pdo = new \ORM();

    if (isset($_POST['username'])) {
        $username = $_POST['username'];
        $users = $pdo->read('SELECT * FROM users');
        var_dump($username);
        echo('<br>');
        var_dump($users);
        die();
    }

编辑:(根据用户要求)

signin.html

<!doctype html>
<html class="fixed">
    <head>
        <meta charset="UTF-8">
        <title>Signin</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800|Shadows+Into+Light" rel="stylesheet" type="text/css" />
        <link rel="stylesheet" href="theme/assets/vendor/bootstrap/css/bootstrap.css" />
        <link rel="stylesheet" href="theme/assets/vendor/font-awesome/css/font-awesome.css" />
        <link rel="stylesheet" href="theme/assets/vendor/magnific-popup/magnific-popup.css" />
        <link rel="stylesheet" href="theme/assets/vendor/bootstrap-datepicker/css/bootstrap-datepicker3.css" />
        <link rel="stylesheet" href="theme/assets/stylesheets/theme.css" />
        <link rel="stylesheet" href="theme/assets/stylesheets/skins/default.css" />
        <link rel="stylesheet" href="theme/assets/stylesheets/theme-custom.css">
        <script src="theme/assets/vendor/modernizr/modernizr.js"></script>
    </head>
    <body>
    <section class="body-sign">
        <div class="center-sign">
            <a href="https://www.patreon.com/bePatron?u=2708372&rid=1056817"><img src="theme/assets/images/buy-now.png" alt="Subscribe by visiting our Patreon page" style="margin-left:55px" /></a>
            <div class="panel panel-sign">
                <div class="panel-title-sign mt-xl text-right">
                    <h2 class="title text-uppercase text-weight-bold m-none"><i class="fa fa-user mr-xs"></i> Sign In</h2>
                </div>
                <div class="panel-body">
                    <form action="index.php" method="POST">
                        <div class="form-group mb-lg">
                            <label>Username</label>
                            <div class="input-group input-group-icon">
                                <input name="username" type="text" class="form-control input-lg" />
                                <span class="input-group-addon">
                                    <span class="icon icon-lg">
                                        <i class="fa fa-user"></i>
                                    </span>
                                </span>
                            </div>
                        </div>
                        <div class="form-group mb-lg">
                            <div class="clearfix">
                                <label class="pull-left">Password</label>
                                <a href="reset.php" class="pull-right">Lost Password?</a>
                            </div>
                            <div class="input-group input-group-icon">
                                <input name="pwd" type="password" class="form-control input-lg" />
                                <span class="input-group-addon">
                                    <span class="icon icon-lg">
                                        <i class="fa fa-lock"></i>
                                    </span>
                                </span>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-sm-8">
                                <div class="checkbox-custom checkbox-default">
                                    <input id="RememberMe" name="rememberme" type="checkbox"/>
                                    <label for="RememberMe">Remember Me</label>
                                </div>
                            </div>
                            <div class="col-sm-4 text-right">
                                <button type="submit" class="btn btn-primary hidden-xs">Sign In</button>
                                <button type="submit" class="btn btn-primary btn-block btn-lg visible-xs mt-lg">Sign In</button>
                            </div>
                        </div>
                        <hr />
                        <p class="text-center">Don't have an account yet? <a href="signup.php">Sign Up!</a></p>
                    </form>
                </div>
            </div>
            <p class="text-center text-muted mt-md mb-md">&copy; Copyright 2016. All Rights Reserved.</p>
        </div>
    </section
    <script src="theme/assets/vendor/jquery/jquery.js"></script>
    <script src="theme/assets/vendor/jquery-browser-mobile/jquery.browser.mobile.js"></script>
    <script src="theme/assets/vendor/bootstrap/js/bootstrap.js"></script>
    <script src="theme/assets/vendor/nanoscroller/nanoscroller.js"></script>
    <script src="theme/assets/vendor/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
    <script src="theme/assets/vendor/magnific-popup/jquery.magnific-popup.js"></script>
    <script src="theme/assets/vendor/jquery-placeholder/jquery-placeholder.js"></script>
    <script src="theme/assets/javascripts/theme.js"></script>
    <script src="theme/assets/javascripts/theme.custom.js"></script>
    <script src="theme/assets/javascripts/theme.init.js"></script>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您的代码中的

$record是一个数组数组,因此为了迭代返回的数据集的每一行,您需要执行以下操作:

 public function read($query) {
        try {
            $this->query = $this->pdo->query($query);

            $users = [];
            $records = $this->query->fetchAll(\PDO::FETCH_ASSOC);


                foreach( $records as $record )
                {
                   //add users here using $record['is_admin']...                     
                }

            return $users;
        } catch(\PDOException $e) {
            die($e);
        }
    }