-2

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);  
          }  
        }       

?>  
  • In your db.class.php use `$conn= mysql_connect($this->db_host, $this->db_user, $this->db_password) or die(mysql_error()); mysql_select_db($this->db_name) or die(mysql_error());` to see if there is an error during connecting to the db and post it here if there is any. – TiMESPLiNTER Nov 13 '13 at 11:22
  • it is connecting to db properly because when I m using primary key field name as id in user table it's working but I have to connect user table with students and teachers table with a foreign key and both tables have a field name id as primary key so I renamed it as user_id @TiMESPLiNTER – user2745286 Nov 13 '13 at 16:50

1 Answers1

1

It seems that you are using the scream extension.

From the manual:

The scream extension gives the possibility to disable the silencing error control operator so all errors are being reported. This feature is controlled by an ini setting.

Scream is an extension for debugging that aims to display as many error messages as possible. This is done by ignoring the @ operator and always having the highest error_reporating level available. (regardless of your error_reporting setting). So you will have to deactivate the extension in your php.ini:

scream.enabled = off

BTW: I would not update a server having 170 websites with code errors. It's a ticking bomb now. It would be better to migrate them site by site to the new PHP version. Maybe having two servers parallel during the migration process.

zzlalani
  • 21,360
  • 16
  • 42
  • 72
  • I couldn't get a way to solve my problem so I changed the field name user_id as id in user table and field name user_id as id and id as s.no in both student and teacher table. May be a little bit confusing but it solved my problem. thanks @innovative kundan and zzlalani for replying – user2745286 Nov 19 '13 at 07:37