本文介绍了在Php类中的空变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php的新手,所以我有一个非常严重的问题。我试着写类,将帮助我与DB工作。代码在这里:

I'm very new in php, so I have a very studid problem. I try to write class that will help me with work with DB. Code here:

<?php
    class DbHelper{
        private $databaseURL;
        private $databaseUName;
        private $databasePWord;
        private $databaseName;
        private $nameOfDbWithWorkers;
        private $connection;

        function __construct($dbURL, $dbUserName, $dbPword, $dbName, $nameOfDbWithWorkers){
            $this->databaseURL = $dbURL;
            $this->databaseUName = $dbUserName;
            $this->databasePWord = $dbPword;
            $this->databaseName = $dbName;
            $this->nameOfDbWithWorkers = $nameOfDbWithWorkers;
        }

        function setConnectionToDb(){
            $connection = mysql_connect($this->databaseURL,$this->databaseUName,$this->databasePWord) OR DIE("can't connect to DB"); 
            mysql_select_db($this->databaseName, $connection)or die ("Error while connecting to database");
        }

        function getUser($login, $pass){
            $query = "SELECT type FROM $this->nameOfDbWithWorkers WHERE login = '$login' and password = '$pass';"; 
            $queryResult = $this->getDataFromDbByQuery($query);
            echo $queryResult;
            if ((mysql_affected_rows($connection) == 1)){
                $meta = mysql_fetch_assoc($queryResult);
                if ($meta['type']=='admin'){
                    return 'admin';
                }
                if ($meta['type']=='user'){
                    return 'user';
                }
                else{
                    return 'nomatch';
                }
            }
            else{               
                 return 'nomatch';
            }           
        }

        function getDataFromDbByQuery($query){
            $this->setConnectionToDb();
            $result = mysql_query($query);
            mysql_close($connection);
            return $result;
        }
}
?>

我调用 getUser 方法,问题 - 中的 $ connection = null 方法和 getDataFromDbByQuery 。为什么这样?

I invoke getUser method, but have a problem - $connection = null in getUser method and in getDataFromDbByQuery. Why so?

推荐答案

因为一个简单的变量总是局部的(除非你使用 global ,但不要习惯这个;))。你可能寻找的是一个财产。你已经定义了 private $ connection ,但是在你的方法中只使用 $ connection ,你不使用它,正尝试访问未定义的局部变量

Because a simple variable is always local (except you make it global using global, but don't get used to this ;)). What you are probably looking for is a property. You already defined private $connection, but just with $connection within your methods you don't use it, but you are trying to access an undefined local variable

    function setConnectionToDb(){
        $this->connection = mysql_connect($this->databaseURL,$this->databaseUName,$this->databasePWord) OR DIE("can't connect to DB"); 
        mysql_select_db($this->databaseName, $this->connection)or die ("Error while connecting to database");
    }

    //..
    function getUser($login, $pass){
        $query = "SELECT type FROM $this->nameOfDbWithWorkers WHERE login = '$login' and password = '$pass';"; 
        $queryResult = $this->getDataFromDbByQuery($query);
        echo $queryResult;
        if ((mysql_affected_rows($this->connection) == 1)){
            // ...
        }
    }

等。只需将 $ connection 替换为 $ this-> connection

这篇关于在Php类中的空变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 07:26