PDO类不在其他类中执行

时间:2014-10-28 09:44:50

标签: php mysql pdo

我有一个包含多个类的PHP文件,可以处理我网站的所有请求。请记住,请求将来自多个平台,而不仅仅是浏览器,我需要具备灵活的功能。

我有一个名为account的班级。这应该是跟踪所有用户数据和信息

class account
{
    public $response = array();

    public function account_login()
    {
        $database = new database();
        $core = new core();
        $needed_fields = array("username","password");
        $return = array();
        $check = validate_field($needed_fields,$return);
        if($check == true){
            $username = $_REQUEST['username'];
            $password = $_REQUEST['password'];

            $query = "select * from user where username='$username' and password='$password'";
            $user_row = $database ->run_query($query,true);
        } else {
            $this->response = $return;
            print_r($check);
            return;
        }
    }
}

这应该使用另一个名为database的类来执行查询并返回结果:

class database
{
    private $database = 'competition';
    private $user = 'root';
    private $password = '1234';
    private $server = 'localhost:3306';
    public $response = array();

    public function run_query($query, $want_result = true, $multiple = false,$inc_history = false)
    {
        if ($want_result === true && $multiple === true) {
            $error = "Cannot execute query!\r\nReason: Cant expect results and execute multiple.\r\nResolution: Change Code for execution.\r\nQuery Below:\r\n$query";
            $file_name = create_error($error);
            $return = array("command" => "message", "message" => "An unexpected error occurred.\r\nError Code - $file_name");
            array_push($this->response, $return);
            return false;
        }

        try {
            $conn = new PDO("mysql:host=$this->server;dbname=$this->database", $this->user, $this->password);
            // set the PDO error mode to exception
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            // begin the transaction
            $conn->beginTransaction();
            // our SQL statememtns
            if ($multiple === true) {
                foreach ($query as $sql) {
                    $sql = addslashes($sql);
                    $conn->exec($sql);
                }
            } elseif ($multiple === false) {
                if ($want_result === false) {
                    $conn->exec($query);
                } elseif ($want_result === true) {
                    if($inc_history === false){
                        $from_pos = stripos($query," from ");
                        $first_space = stripos($query," from ",$from_pos + 6);
                        $from_table = substr($query,$from_pos+6,$from_pos+6 - $first_space);

                        if(stripos($query," where ") === false){
                            if(stripos($query," order by ") === false){
                                if(stripos($query," group by ") === false){
                                    $query .= "\r\n where `$from_table`.history = 0 ";
                                } else {
                                    $query = str_ireplace(" group by "," where `$from_table`.history = 0 \r\n group by ",$query);
                                }
                            } else {
                                $query = str_ireplace(" order by "," where `$from_table`.history = 0 \r\n order by ",$query);
                            }
                        } else {
                            $query = str_ireplace(" where "," where `$from_table`.history = 0 \r\n and ",$query);
                        }
                    }
                    die($query);

                    $stmt = $conn->prepare($query);
                    $stmt->execute();

                    // set the resulting array to associative
                    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

                    $table = array();
                    $result = $stmt->fetchAll();
                    foreach($result as $row){
                        foreach(array_keys($result) as $key){
                            $table[$key] = $result[$key];
                        }
                    }
                    $conn = null;
                    return $table;
                } // want result
            } // multiple

            // commit the transaction
            $conn->commit();
            return true;
        } catch (PDOException $e) {
            // roll back the transaction if something failed
            $conn->rollback();

            $error = "MESSAGE\r\n==========================\r\n";
            $error .= $e->getMessage();
            $error .= "\r\nCALL STACK\r\n==========================\r\n";
            $error .= $e->get_call_stack();
            $error .= "\r\nQuery\r\n==========================\r\n";
            $error .= print_r($query,true);

            $file_name = create_error($error);

            $return = array("command" => "message", "message" => "An unexpected error occurred.\r\nError Code - $file_name");
            array_push($this->response, $return);

            return false;
        }
        $conn = null;
    }
}

然后,当我尝试很长时间(在浏览器中)时,我收到以下错误:

Fatal error: Class 'core_system\PDO' not found in C:\Web\dev\backend\core\core.php on line 92

但是,如果我将连接添加到另一个没有使用类的php文件,它连接没有问题。 我查看了谷歌和stackoverflow,看看是否有其他人有同样的问题,但看起来好像其他人的PDO在类中连接。

我已经检查过并且在我的php.ini文件中启用了extension=c:\web\php\php_pdo_mysql.dll,我的apache也启动时没有任何错误。

您看到的其他功能是:

function create_error($error_text)
{
    $now = date('Ymd.His');
    $file_name = $now . md5($now);

    $error_text .= "\r\n\r\nSERVER\r\n==========================\r\n";
    $error_text .= print_r($_SERVER, true);
    $error_text .= "\r\n\r\nSESSION\r\n==========================\r\n";
    $error_text .= print_r($_SESSION, true);
    $error_text .= "\r\n\r\nREQUEST\r\n==========================\r\n";
    $error_text .= print_r($_REQUEST, true);

    file_put_contents("C:\Web\Error\\$file_name", $error_text);
    return $file_name;
}

function validate_field($needed_fields,$return){
    foreach($needed_fields as $field){
        if(isset($_REQUEST[$field])===false || strlen($_REQUEST[$field]) === 0){
            $field = ucwords($field);
            array_push($return,array("command"=>"message","message"=>"$field is required."));
        }
    }

    if(count($return) === 0){
        return true;
    } else {
        return false;
    }
}

0 个答案:

没有答案
相关问题