0

I have a PHP 5.6 compatible script. Now in my server i have to change to php7.2, but this script get error everywhere.I want to convert it myself but my knowledge does not reach much. I have been advancing little by little with several tutorials but I have already been stuck without being able to continue.

I need help to do it myself. This is just an example of the code, but by solving this, I think I will be able to convert the whole site.

The error:

   [01-Nov-2018 09:16:58 UTC] PHP Fatal error:  Uncaught Error: Call to a member function query() on null in /home/xxxx/public_html/incs/generales/fullnews.php:433
Stack trace:
#0 /home/xxxx/public_html/news.php(16): FullNewsInc->getFullNews('18880', 'fullnews')
#1 {main}
  thrown in /home/xxxx/public_html/incs/generales/fullnews.php on line 433

the news.php page

  <?php   
require_once './db.php'; $db = new DB_Sql(); $db2 = new DB_Sql();  
include_once './fullnews.php';
$fullnewsinc = new FullNewsInc($db); 
$newsId = $_REQUEST['newsId'];
$system = $_REQUEST['system'];
$cat = $_REQUEST['categorie']; 

?> 
    <section id="main-content" itemscope itemtype="https://schema.org/Article">  
        <div id="latest">
            <section class="noticias"> 
<div class="clearfix"></div>   
 <?php $fullnewsinc->getFullNews($newsId, 'fullnews'); ?>  

            </section>   

            <aside class="sidebar">  
                        </aside>
        </div> <!-- END Latest -->
        <div class="clearfix"></div> 
    </section>   

This is the function in fullnews.php

<?php

class FullNewsInc {

    var $db;
 public function __construct()
    {
        // Constructor's functionality here, if you have any.
    }

    public function FullNewsInc(&$db) {

        $this->db = &$db;  self::__construct();

    } 

    function getFullNews($newsId = '', $template = 'theme_fullnews') {

        $sql = "SELECT x FROM";

        $this->db->query($sql);

        while($this->db->next_record()) {
            $newsId = $this->db->f("newsId");
            $subject = $this->db->f("subject");
            $shortNews = $this->db->f("shortNews");
            $longNews = $this->db->f("longNews");
            $iconId = $this->db->f("iconId");
            $source = $this->db->f("source");
            include "./theme/".$template.".tpl.php";

        }

    }


}?>

This is the db.php

<?php 
if (!class_exists('DB_Sql')){  
class DB_Sql { 

  public function __construct()
    {
        // Constructor's public functionality here, if you have any.
    }
     public $Host     = "xxx";
  public $Database = "xxxx";
  public $User     = "xxx";
  public $Password = "xxxxx";
  public $Auto_Free     = 1;
  public $Debug         = 0;
  public $Halt_On_Error = "no"; // "yes", "no", "report"
  public $Record   = array();
  public $Row;
  public $Errno    = 0;
  public $Error    = "";
  public $Link_ID  = 0;
  public $Query_ID = 0;

  public function DB_Sql($query = "") {
      $this->query($query);self::__construct();
  }

  public function link_id() {
    return $this->Link_ID;self::__construct();
  }

  public function query_id() {
    return $this->Query_ID;self::__construct();
  }

  public function connect($Database = "", $Host = "", $User = "", $Password = "") {
    if ("" == $Database)
      $Database = $this->Database;
    if ("" == $Host)
      $Host     = $this->Host;
    if ("" == $User)
      $User     = $this->User;
    if ("" == $Password)
      $Password = $this->Password;

    if ( 0 == $this->Link_ID ) {
      $this->Link_ID=@mysqli_connect($Host, $User, $Password, $Database);
      if (!$this->Link_ID) {
        $this->halt("connect failed - Please check your database settings.");
        die;
      }

      if (!@mysqli_select_db($Database,$this->Link_ID)) {
        $this->halt("cannot use database ".$this->Database." - Please check your database settings.");
        die;
      }
    }

    return $this->Link_ID;
  }

  public function free() {
      @mysql_free_result($this->Query_ID);
      $this->Query_ID = 0;self::__construct();
  }

  public function query($Query_String) {
    if ($Query_String == "") {
      return 0;
    }

    if (!$this->connect()) {
      return 0;
    }

    # New query, discard previous result.
    if ($this->Query_ID) {
      $this->free();
    }

    if ($this->Debug)
      printf("Debug: query = %s<br />\n", $Query_String);

    $this->Query_ID = @mysqli_connect($Query_String,$this->Link_ID);
    $this->Row   = 0;
    $this->Errno = mysql_errno();
    $this->Error = mysql_error();
    if (!$this->Query_ID) {
      $this->halt("Invalid SQL: ".$Query_String);
    }

    return $this->Query_ID;self::__construct();
  }

  public function next_record() {
    if (!$this->Query_ID) {
      $this->halt("next_record called with no query pending.");
      return 0;
    }

    $this->Record = @mysqli_fetch_array($this->Query_ID);
    $this->Row   += 1;
    $this->Errno  = mysql_errno();
    $this->Error  = mysql_error();

    $stat = is_array($this->Record);
    if (!$stat && $this->Auto_Free) {
      $this->free();
    }
    return $stat;self::__construct();
  }

  public function affected_rows() {
    return @mysql_affected_rows($this->Link_ID);self::__construct();
  }

  public function num_rows() {
    return @mysql_num_rows($this->Query_ID);self::__construct();
  }

  public function num_fields() {
    return @mysql_num_fields($this->Query_ID);self::__construct();
  }

  public function nf() {
    return $this->num_rows();self::__construct();
  }

  public function np() {
    print $this->num_rows();self::__construct();
  }

  public function f($Name) {
    return $this->Record[$Name];self::__construct();
  }

  public function p($Name) {
    print $this->Record[$Name];self::__construct();
  }

  public function halt($msg) {
    $this->Error = @mysqli_error($this->Link_ID);
    $this->Errno = @mysqli_errno($this->Link_ID);
    if ($this->Halt_On_Error == "no")
      return;

    $this->haltmsg($msg);

    if ($this->Halt_On_Error != "report")
      die("Session halted.");self::__construct();
  }

  public function haltmsg($msg) {
    printf("<b>Database error:</b> %s<br />\n", $msg);
    printf("<b>MySQL Error</b>: %s (%s)<br />\n",
      $this->Errno,
      $this->Error);self::__construct();
  }
}

}

The theme_fullnews tamplate:

<article>
<header>   
  <h1 itemprop="name" ><?php echo $subject; ?></h1> 
  <h2 itemprop="articleSection"><?php echo $shortNews; ?></h2>

  <span itemprop="datePublished" content=" <?php echo $newDate; ?>">  <?php   echo time_elapsed_string($timestamp);   ?> </span>


    </div>
  </header>

<section>   


    <?php $longNews); ?>


</section>

</article>
Jota
  • 1
  • 2
  • 3
    The warning really says it all: Instead of relying on the method that has the same name as the class to auto-run when you generate an object, rename it to `__construct` and remove the call to the empty constructor and the empty constructor itself. – jeroen Nov 01 '18 at 09:04
  • also, this: `var $db;` should be `public $db` - see here: https://stackoverflow.com/questions/1206105/what-does-php-keyword-var-do – treyBake Nov 01 '18 at 09:07
  • I have change this thing, and i get new error whit the query(); function. I updated the topic. – Jota Nov 01 '18 at 09:13
  • @Jota, pls also post code of `/home/phpmanitaswp/public_html/incs/generales/inc_fullnews.inc.php`, I also see a wrong spelling of constructor its __construct not __contruct in db.php – Dharmang Nov 01 '18 at 09:15
  • as @harvey-fletcher mentioned, move your logic to `__construct` from `FullNewsInc` function, you expect the function getting called but it doesn't according to the standards, so you are getting error: "Call to a member function query() on null" as $db is never assigned a value as you expect. – Dharmang Nov 01 '18 at 09:23

1 Answers1

0

What's happening here is that you have functions inside your class with the same name as the class. PHP is warning you that the ability to do this is going to be removed in future.

For example, you have your php class

<?php

class FullNewsInc {
    public function __construct( )
    {
        // Constructor's public functionality here, if you have any.

    }

    /**
        Some code here
    **/

    public function FullNewsInc($db){
        $this->db = &$db;
    }

    /**
        More code here
    **/
}

The class and method name match, so you would need to change your PHP class to use the __construct method, which you've written, but it is currently blank.

class FullNewsInc {
    public function __construct( $db )
    {
        // Constructor's public functionality here, if you have any.
        $this->db = &$db;
    }

    /**
      Some code here
    **/

    //The FullNewsInc method has been deleted. The code contained within was moved to the __construct

    /**
      More code here
    **/
}

There is more information available on the php.net site http://php.net/manual/en/language.oop5.decon.php

Harvey Fletcher
  • 1,167
  • 1
  • 9
  • 22
  • I fix this, and now the error is diferent :/ Some error whit the query function. – Jota Nov 01 '18 at 09:22
  • I update the topic: `[01-Nov-2018 09:16:58 UTC] PHP Fatal error: Uncaught Error: Call to a member function query() on null in /home/xxxx/public_html/incs/generales/fullnews.php:433 Stack trace: #0 /home/xxxx/public_html/news.php(16): FullNewsInc->getFullNews('18880', 'fullnews') #1 {main} thrown in /home/xxxx/public_html/incs/generales/fullnews.php on line 433` – Jota Nov 01 '18 at 09:23
  • @Jota, you need to move your logic to `__construct` see my comment above. – Dharmang Nov 01 '18 at 09:25
  • OK, this is because the line `$this->db->query($sql);` is throwing an error in `function getFullNews()` this could be because your database is not connected properly. I see that you have the `var $db` in the class, but it is `NULL`. You need to set up the variable using `$dbs = new DB_Sql(); $db = $dbs->connect();` – Harvey Fletcher Nov 01 '18 at 09:28