php pdostament获取问题

时间:2013-03-08 17:11:13

标签: php pdo pdostatement

我有一个数据库,有两个用于测试目的的记录...... 数据库列(user_id,用户名,密码和角色)....

我正在使用PHP PDO与数据库进行通信我创建了一个扩展PDO的类文件,该文件位于

之下
<?php
    require_once('constants.php');
    class PDOConfig extends PDO{

        private $real_escape_string;
        private $mageic_quote_active;


        public function __construct(){
            $dsn = DBENGINE .':dbname='. DBNAME.";host=".DBHOST; 
            parent::__construct($dsn, DBUSER, DBPW);
            $mageic_quote_active = get_magic_quotes_gpc();
            $real_escape_string = function_exists("mysql_real_escape_string");
        }


        public function escapeValue($value){
            //PHP >= 4.3.0 or greater
            if($this->real_escape_string){
                if($this->mageic_quote_active){
                    //if new version exists turn magic quotes off
                    $value = stripslashes($value);
                }
                $value = mysql_real_escape_string($value);
            }
            return $value;
        }

    }//end of class

    $connection = new PDOConfig();
    $database =& $connection;

我在登录页面上使用此文件,其中包含以下代码

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Sign In</title>
        <link rel="stylesheet" href="css/bootstrap.css">
        <link rel="stylesheet" href="css/singin-form.css">

        <?php require_once('includes/PDOConfig-class.php'); ?>
        <?php //require_once('includes/utility-functions.php'); ?>
        <?php
            $errors = "";
            if(isset($_POST['submit'])){

                $username = trim($_POST['username']);
                $password = trim($_POST['password']);

                if(!empty($username) && !empty($password)){
                    $sql = "SELECT * FROM user WHERE user_name = '" . $database->escapeValue($username) . "'";
                    $prestmt = $database->prepare($sql);
                    $prestmt->execute();
                    $result = $prestmt->fetch($database::FETCH_ASSOC);
                    //print_r($result);
                    echo $result['role'];


                    echo "<br /> Query :  " . $sql;
                }
                //echo '<div class="alert alert-error">something wrong<button type="button" class="close" data-dismiss="alert">&times;</button></div>';
            }

        ?>


    </head>
    <body>

        <div class="container">

            <form class="form-signin" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
                <h2>Please sign in</h2>
                <input type="text" name="username" class="input-block-level" placeholder="User Name" />
                <input type="password" name="password" class="input-block-level" placeholder="Password" />
                <button name="submit" class="btn btn-primary btn-large" type="submit">Login</button>
            </form>
        </div>

    </body>
</html>

但我在我打印的那个查询旁边看不到任何内容......虽然数据库中有数据......

另一件事,当我尝试“FETCH_OBJ”使用时,我得到以下错误

trying to get property of non-object in C:\wamp\www\testPHP\login.php on line 30

产生上述错误的代码低于

if(!empty($username) && !empty($password)){
    $sql = "SELECT * FROM user WHERE user_name = '" . $database->escapeValue($username) . "'";
    $prestmt = $database->prepare($sql);
    $prestmt->execute();
    $result = $prestmt->fetch($database::FETCH_OBJ);
    echo $result->role;
}

请帮忙,因为我从来没有使用过PHP PDO而且在php中也不是很好...感谢您的时间

1 个答案:

答案 0 :(得分:1)

This link包含您需要的一切

  • 按照那里描述的方式连接到db(让PDO通知你错误)
  • 按照那里描述的方式为查询分配变量(以使您的查询安全且合理)
  • 摆脱你的PDOConfig,因为它完全没用。你只需要构造函数,因此,你可以将构造函数本身放入PDOConfig-class.php

然后运行你的代码,看看出了什么问题