我为学生和老师准备了两个不同的表(因为它们都有不同的字段),主键 id 并且所有页面都正确打开,但我读到拥有两个不同的登录表单不是一个好习惯,所以我创建了一个包含一些常见的用户表具有主键 user_id 的教师和学生的字段。user_id 是学生表和教师表的外键。所有页面都包含一个 global.inc 文件。当我在 user.class.php 文件中提供一个 ID 时,如 $id=15; 脚本正常工作,否则所有页面上都会显示以下错误。有人可以帮忙吗?
( ! ) SCREAM: Error suppression ignored for
( ! ) Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given
in C:\wamp\www\curri\web\classes\db.class.php on line 44
Call Stack
# Time Memory Function Location
1 0.0008 251456 {main}( ) ..\dashboard.php:0
2 0.0055 257088 require_once( 'C:\wamp\www\curri\web\global.inc.php' )
..\dashboard.php:1
3 1.0224 378496 usertools->get( ) ..\global.inc.php:20
4 1.0224 379224 DB->select( ) ..\usertools.class.php:56
5 1.0229 379704 mysql_num_rows ( ) ..\db.class.php:44
( ! ) SCREAM: Error suppression ignored for
( ! ) Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given
in C:\wamp\www\curri\web\classes\db.class.php on line 26
Call Stack
# Time Memory Function Location
1 0.0008 251456 {main}( ) ..\dashboard.php:0
2 0.0055 257088 require_once( 'C:\wamp\www\curri\web\global.inc.php' )
..\dashboard.php:1
3 1.0224 378496 usertools->get( ) ..\global.inc.php:20
4 1.0224 379224 DB->select( ) ..\usertools.class.php:56
5 1.0232 379656 DB->processRowSet( ) ..\db.class.php:47
6 1.0232 379912 mysql_fetch_assoc ( ) ..\db.class.php:26
这些是包含在所有页面上的文件。
global.inc.php----
<?php
require_once ("classes/user.class.php");
require_once ("classes/usertools.class.php");
require_once ("classes/db.class.php");
$db = new DB();
$db->connect();
$usertools = new usertools();
session_start();
if(isset($_SESSION['logged_in'])) {
$user = unserialize($_SESSION['user']);
$_SESSION['user'] = serialize($usertools->get($user->id));
}
?>
usertools.class.php-----
<?php
require_once ("user.class.php");
require_once ("db.class.php");
class usertools {
public function login($username, $password, $usertype)
{
global $table;
$result = mysql_query("SELECT * FROM $table WHERE user_name='$username'
&& password='$password' && user_type='$usertype'");
if(mysql_num_rows($result) > 0)
{
$_SESSION["user"] = serialize(new User(mysql_fetch_assoc($result)));
$_SESSION["login_time"] = time();
$_SESSION["logged_in"] = 1;
return true;
}else{
return false;
}
}
//returns a User object. Takes the users id as an input
public function get($id)
{
$db = new DB();
$result = $db->select('user', "user_id = $id");
return new User($result);
}
}
?>
user.class.php---
<?php
require_once ("db.class.php");
class User {
public $id;
.
.
public $usertype;
function __construct($data) {
$this->id = (isset($data['id'])) ? $data['id'] : "";
.
.
$this->usertype = (isset($data['user_type'])) ? $data['user_type'] : "";
}
public function saveteacher($isNewUser = false) {
global $table;
$db = new DB();
$data = array(
"user_name" => "'$this->username'",
"email" => "'$this->email'",
.
.
.
"user_type" => "'$this->usertype'" );
$this->id = $db->insert($data, 'user');
$data1 = array(
"user_id" => "'$this->id'",
"teacher_id" => "'$this->teacher_id'",
.
.
"subject" => "'$this->subject'",
);
$db->insert($data1, 'teachers');
}
return true;
}
public function savestudent($isNewUser = false) {
}
}
?>
db.class.php---
<?php
class DB {
protected $db_host = "localhost";
protected $db_user = "root";
protected $db_password = "";
protected $db_name = "db_name";
public $table;
public function connect() {
$conn= mysql_connect($this->db_host, $this->db_user, $this->db_password);
mysql_select_db($this->db_name);
return true;
}
public function processRowSet($rowSet, $singleRow=false)
{
$resultArray = array();
while($row = mysql_fetch_assoc($rowSet))
{
array_push($resultArray, $row);
}
if($singleRow === true)
return $resultArray[0];
return $resultArray;
}
public function select($table, $where) {
$sql = "SELECT * FROM $table WHERE $where";
$result = mysql_query($sql);
if(mysql_num_rows($result) == 1)
return $this->processRowSet($result, true);
return $this->processRowSet($result);
}
}
?>