dsc80135 2018-11-01 09:00
浏览 89

将PHP 5.6新闻脚本转换为7.2

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 />
", $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 />
", $msg);
    printf("<b>MySQL Error</b>: %s (%s)<br />
",
      $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>
  • 写回答

1条回答 默认 最新

  • duanjiani6826 2018-11-01 09:18
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!