0

We upgraded our php from 5.3 to 5.6 and now I'm getting an errors and warnings. Im a bit confused on what as deprecated from php 5.3 that would be causing this.

PHP Warning:  get_class() called without object from outside a class in /home/website/public_html/php/search.class.php on line 6

PHP Warning:  Creating default object from empty value in /home/website/public_html/favorites/favorite.class.php on line 25

PHP Fatal error:  Call to a member function getEntry() on a non-object in /home/website/public_html/php/search.class.php on line 43  

The get_class warning

if( get_class($favorite) === false )
    $favorite = new favorite;

$commQuery is the fatal error line 43

  class getCommDetails{
   var $community;
   var $commURL;
   var $commAddress;
   var $commPhone;
   var $cid;
   var $commMin;
   var $commMax;

function getCommDetails($community){
    $ret = false;
    if( get_class($db) === false )
            $db = new DB(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
    $this->community = $community;

     $commQuery = $db->getEntry('communities', '*', "communities.community =     '$this->community'");

    if( $db->numrows($commQuery) === 1) {
        $commResults = $db->fetch_array($commQuery);
        $this->commURL = $commResults['url'];
        $this->commAddress = $commResults['address'];
        $this->commPhone = $commResults['sales_phone'];
        $this->cid = $commResults['cid'];
        $this->vars = $commResults;

        //$this->commURL = $fpResults
        $ret = true;
    }
    return $ret;
}
};

Database connection code

    <?php
 class DB {
// Class Variables
var $connection;
var $queryStr;

// Class Constructor
function DB($DB_Server, $DB_User, $DB_Pass, $DB_Name){
    $this->connection = mysqli_connect("$DB_Server", "$DB_User", "$DB_Pass","$DB_Name") or die("MySQL Connection Err: ". mysqli_error($this->connection));
    //mysql_select_db("$DB_Name") or die("MySQL DB Err: ".mysql_error());
}

// Class Functions
function addEntry($table, $fieldlist, $valuelist){
    $this->queryStr = "INSERT INTO `". $table ."` ($fieldlist) VALUES ($valuelist)";
    return $this->query($this->queryStr);
}

function updateEntry($table, $fieldvalues, $where = NULL){
    if( is_array($fieldvalues) ){
        $c=0;$numFields=count($fieldvalues);
        foreach($fieldvalues as $fieldname => $value){
            if($fieldname == 'inventoryAction'){
                $c++;
                continue;
            }
            $fieldvaluelist .= $fieldname."='".$value."'";
            if($numFields > 1){
                if($c < $numFields-1)
                    $fieldvaluelist .= ",";
            }
            $c++;
        }
    } else {
        return false;
    }
    $this->queryStr = "UPDATE `". $table ."` SET ".$fieldvaluelist;
    if($where !== NULL)
        $this->queryStr .= " WHERE $where";
    return $this->query($this->queryStr);
}

function removeEntry($table, $where = NULL){
    if($where != NULL){
        $this->queryStr = "DELETE FROM `". $table ."` WHERE $where";
        return $this->query($this->queryStr);
    } else {
        return false;
    }
}

  function getEntry($table, $fieldlist, $where=NULL, $whereon=NULL, $groupby = NULL){
    $this->queryStr = "SELECT $fieldlist FROM ".$table;
    if($whereon !== NULL)
        $this->queryStr .= " ON $whereon";
    if($where !== NULL)
        $this->queryStr .= " WHERE $where";
    if($groupby !== NULL)
        $this->queryStr .= " GROUP BY $groupby";

    return $this->query($this->queryStr);
}

function fetch_array($qResults, $recordType = NULL){
    if($recordType == "MYSQL_NUM"){
        return mysqli_fetch_array($qResults, MYSQL_NUM);
    } elseif($recordType == "MYSQL_ASSOC"){
        return mysqli_fetch_array($qResults, MYSQL_ASSOC);
    } else {
        return mysqli_fetch_array($qResults);
    }
}

function fetch_object($qResults){
    return mysqli_fetch_object($qResults);
}

function numrows($qResults){
    if(!$qResults)
        return false;
    else
        return mysqli_num_rows($qResults);
}

function getLastID(){
    return mysqli_insert_id($this->connection);
}

function getQuery(){
    return $this->queryStr;
}

function query($query){
    if(!$query)
        return false;
    else
        return mysqli_query($this->connection,$query);
}
 };
 ?>
zero298
  • 25,467
  • 10
  • 75
  • 100
user3369825
  • 99
  • 3
  • 12
  • I formatted your warnings, fixed some of your capitalization, and made you boldness consistent. You should try to cut down some of your code to the pertinent points though. – zero298 Dec 31 '15 at 22:35
  • To answer why you see some of these errors - `$db` is not defined in function scope at the time you use `get_class()`. In your old setup, you may have not had error_reporting up to E_ALL, because this would have generated `E_NOTICE undefined variable $db`. The "default object from empty value" happens now because if you did have error_reporting at E_ALL, prior to 5.4 it did not include E_STRICT warnings, but later does. That is a strict violation. http://stackoverflow.com/questions/8900701/creating-default-object-from-empty-value-in-php – Michael Berkowski Dec 31 '15 at 23:20

1 Answers1

1

Remove condition

if( get_class($db) === false )

Try to write without if:

        $db = new DB(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
Nick
  • 9,735
  • 7
  • 59
  • 89