class.mysql.php
有2類: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);
}
}
login.php
從form
被submitted
後檢查username
和password
。如果字段爲not empty
check database
爲條目。如果有條目,則設置會話。
的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="password" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
所以這裏的問題,即時通訊正在使用的login.php此代碼來檢查數據庫條目:
if ($db->size() == 1)
這段代碼顯然是錯誤的,因爲$db
是MySQL class
的一個對象,但size() method
在MySQLResult class.
有沒有簡單的方法可以到size()?
在此先感謝
這是我的錯誤:
Fatal error: Call to undefined method MySQL::size()
我認爲那裏有1.000。 000個世界上的Mysql類。 – pltvs 2010-10-31 18:13:54