调用成员函数prepare()php pdo

时间:2015-04-18 07:03:08

标签: php mysql pdo

我有这个类,我初始化PDO对象,但当我尝试连接index.php时,我看到错误。 我检查了全局变量,并将$ db设置为全局,但错误仍然存​​在。

db_functions类:

<?php

    class DB_Functions {

         var $db;

        //put your code here
        // constructor
        function __construct() {
                    require_once 'config.php';
            // global $db ;
                     $username = '****';
                     $password = '****';
            // connecting to mysql
            try {

                $db = new PDO('mysql:host=localhost;dbname=netshope_login',$username , $password);
                $db->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);
                echo 'succes';
                var_dump($db);

            } catch (PDOException $e) {
                echo "Error" . $e->getMessage() ;

            }

       }

        // destructor
        function __destruct() {

        }

        /**
         * Storing new user
         * returns user details
         */

        /**
         * Check user is existed or not
         */
        public function isUserExisted($email) {

           //     $db = new PDO('mysql:host=localhost;dbname=netshope_login',$username , $password);
         //       $db->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);

            $stmt = $db->prepare("SELECT email FROM users WHERE email = :email");
            $stmt->execute(array(
                ':email' =>$email
                ));

            $no_of_rows = $stmt->fetchAll();
            if (count($no_of_rows)) {
                // user existed 
                return true;
            } else {
                // user not existed
                return false;
            }
        }

    }

    ?>

这是我的index.php。 当我试图连接PDO时,我有致命的错误。

<?php
if (isset($_POST['tag']) && $_POST['tag'] != '') {
    // get tag
    $tag = $_POST['tag'];

    // include db handler
    require_once 'include/DB_Functions.php';
    $db = new DB_Functions();

    // response Array
    $response = array("tag" => $tag, "error" => FALSE);

 if ($tag == 'register') {
        // Request type is Register new user
        $name = $_POST['name'];
        $email = $_POST['email'];
        $password = $_POST['password'];

        // check if user is already existed
        if ($db->isUserExisted($email)) {
            // user is already existed - error response
            $response["error"] = TRUE;
            $response["error_msg"] = "User already existed";
            echo json_encode($response);
        }

?>

0 个答案:

没有答案