开发者

Calling method of another class in same file of class being used?

class.mysql.php has 2 classes: MySQL and 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);
    }

}

login.php is checking for username and password after being submitted from a form. If fields are not empty check database for an entry. If there is an entry then set the session.

login.php:

<?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=开发者_StackOverflow社区"password" /><br />
            <input type="submit" name="submit" value="Submit" />
        </form>

    </body>
</html>

So here's the question, im am using this code in login.php to check for database entry:

if ($db->size() == 1)

This code is obviously wrong because $db is an object of MySQL class but the size() method is in the MySQLResult class. Is there a simply way to get to size()? Thanks in advance

This is my error:

Fatal error: Call to undefined method MySQL::size()


$db->query($sql); returns a MySQLResult. You have to do something like

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


I belive that there is a MySQL class MySQLResult class. and some other classes for supporting database connection

you need to access a method in MySQLResult class. without including in the login page

you can include the MySQLResult class. in MySQL class and create a public method to access its functionality

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜