Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正在使用的类的同一文件中调用另一个类的方法?_Php_Oop - Fatal编程技术网

Php 正在使用的类的同一文件中调用另一个类的方法?

Php 正在使用的类的同一文件中调用另一个类的方法?,php,oop,Php,Oop,class.mysql.php有两个类:mysql和MySQLResult class.mysql.php: <?php /** * MySQL Database Connection Class * @access public */ class MySQL { /** * MySQL server hostname * @access private * @var string */ var $host; /**

class.mysql.php
有两个类:
mysql
MySQLResult

class.mysql.php:

<?php

/**
 * MySQL Database Connection Class
 * @access public
 */
class MySQL {

    /**
     * MySQL server hostname
     * @access private
     * @var string
     */
    var $host;
    /**
     * MySQL username
     * @access private
     * @var string
     */
    var $dbUser;
    /**
     * MySQL user's password
     * @access private
     * @var string
     */
    var $dbPass;
    /**
     * Name of database to use
     * @access private
     * @var string
     */
    var $dbName;
    /**
     * MySQL Resource link identifier stored here
     * @access private
     * @var  string
     */
    var $dbConn;
    /**
     * Stores error messages for connection errors
     * @access private
     * @var string
     */
    var $connectError;

    /**
     * MySQL constructor
     * @param string host (MySQL Server hostname)
     * @param string dbUser (MySQL User Name)
     * @param string dbPass (MySQL User Password)
     * @param string dbName (Database to select)
     * @access public
     */
    function MySQL($host, $dbUser, $dbPass, $dbName) {
        $this->host = $host;
        $this->dbUser = $dbUser;
        $this->dbPass = $dbPass;
        $this->dbName = $dbName;
        $this->connectToDb();
    }

    /**
     * Establishes connection to MySQL and selects a database
     * @return void
     * @access private
     */
    function connectToDb() {
        //Make connection to MySQL server
        if (!$this->dbConn = @mysql_connect($this->host, $this->dbUser, $this->dbPass)) {
            trigger_error('Could not connect to server');
            $this->connectError = true;
            //Select database
        } else if (!mysql_select_db($this->dbName, $this->dbConn)) {
            trigger_error('Could not select database');
            $this->connectError = true;
        }
    }

    /**
     * Checks for MySQL errors
     * @return boolean
     * @access public
     */
    function isError() {
        if ($this->connectError) {
            return true;
        }
        $error = mysql_error($this->dbConn);
        if (empty($error)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Returns an instance of MySQLResult to fetch rows with
     * @param $sql string the database query to run
     * @return MySQLResult
     * @access public
     */
    function query($sql) {
        if (!$queryResource = mysql_query($sql, $this->dbConn)) {
            trigger_error('Query failed: ' . mysql_error($this->dbConn) . ' SQL: ' . $sql);
        }
        return new MySQLResult($this, $queryResource);
    }

}

/**
 * MySQLResult Data Fetching Class
 * @access public
 */
class MySQLResult {

    /**
     * Instance of MySQL providing database connection
     * @access private
     * @var MySQL
     */
    var $mysql;
    /**
     * Query resource
     * @access private
     * @var resource
     */
    var $query;

    /**
     * MySQLResult constructor
     * @param object mysql (instance of MySQL class)
     * @param resource query (MySQL query resource)
     * @access public
     */
    function MySQLResult(&$mysql, $query) {
        $this->mysql = &$mysql;
        $this->query = $query;
    }

    /**
     * Fetches a row from the result
     * @return array
     * @access public
     */
    function fetch() {
        if ($row = mysql_fetch_array($this->query, MYSQL_ASSOC)) {
            return $row;
        } else if ($this->size() > 0) {
            mysql_data_seek($this->query, 0);
            return false;
        } else {
            return false;
        }
    }

    /**
     * Checks for MySQL Errors
     * @return boolean
     * @access public
     */
    function isError() {
        return $this->mysql->isError();
    }

    /**
     * Returns the number of rows selected
     * @return int
     * @access public
     */
    function size() {
        return mysql_num_rows($this->query);
    }

    /**
     * Returns the ID of the last row inserted
     * @return int
     * @access public
     */
    function insertID() {
        return mysql_insert_id($this->mysql->dbConn);
    }

}
<?php
//MySQL class
require_once('database/class.mysql.php');

//Session class
require_once('session/class.session.php');

//Instantiate the mysql class
require_once('database/dbconnect.php');
$db = new MySQL($host, $dbUser, $dbPass, $dbName);

//Instantiate the Session class
$session = new Session();

//$session->destroy();
if (!$session->get('username')) {
    if (isset($_POST['submit'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        if (!empty($username) && !empty($password)) {
            $sql = "SELECT * FROM user WHERE username = '$username' AND password = '$password'";
            $db->query($sql);
            if ($db->size() == 1) {
                $session->set('username', $username);
            }
        }
    }
}

if (!$session->get('username')) {
    echo 'not set';
}
?>
<html>
    <head>
    </head>
    <body>


        <form method="post" action="">
            Username: <input type="text" name="username" /><br />
            Password: <input type="text" name="password" /><br />
            <input type="submit" name="submit" value="Submit" />
        </form>

    </body>
</html>
问题是,我在login.php中使用以下代码检查数据库条目:

if ($db->size() == 1)
这段代码显然是错误的,因为
$db
MySQL类的一个对象
,但是
size()方法
MySQLResult类中。
有没有一种简单的方法可以到达
size()?
提前感谢

这是我的错误:

Fatal error: Call to undefined method MySQL::size()
$db->query($sql)
返回一个
MySQLResult
。你必须做一些类似的事情

$result = $db->query($sql);
if ($result->size() == 1) {

我相信有一个
MySQL类
MySQLResult类。
和一些其他类支持数据库连接

您需要访问
MySQLResult类中的方法。
而不包括在登录页面中


您可以在
MySQLResult类中包含
MySQLResult类。
并创建一个
public
方法来访问其功能

我认为世界上有1000.000个MySQL类。