This question already has an answer here:
I had two different tables for students and teachers(because both have different fields) with primary key id and all pages were opening correctly but I read that its not a good practice to have two different login forms so I created a user table with some common field for both teachers and students with primary key user_id . user_id is foreign key for both students table and teachers table. A global.inc file is included on all pages.when I provide an id in user.class.php file like $id=15; script is working properly otherwise following error is showing on all pages.Can somebody help?
( ! ) 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
These are files included on all pages.
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);
}
}
?>
</div>