How to create the same codes to run in php7? [duplicate]

-3

Good people I'm a bit late in things and, until now I could not learn php7 again.

Well those files (3) there I am showing you is the way I do to show database record in a show way. I would like to know if there is any way to create an updated version of these codes.

Or if there is something more current and easy to implement in my projects.

conection.php

<?php


  class xDatabase{


    var $DB_HOSTNAME        = "localhost";
    var $DB_USERNAME        = "user";
    var $DB_PASSWORD        = "";
    var $DB_DATABASE        = "Xbanco";





    var $DB_CONNECTION      = false;

    var $DB_RESULT          = false;
    var $DB_RESULT_ROWS     = 0;   
    var $DB_AFFECTED_ROWS   = 0;
    var $DB_PREV_INSERT_ID  = 0;    

    function xDatabase() {
      $this->DB_CONNECTION = @mysql_connect($this->DB_HOSTNAME, $this->DB_USERNAME, $this->DB_PASSWORD);
      if (!$this->DB_CONNECTION);
      if (!@mysql_select_db($this->DB_DATABASE));
    }

    function query($query) {
      if (!$this->DB_CONNECTION) 
        return false;
      if (!$this->DB_RESULT = @mysql_query($query, $this->DB_CONNECTION))
        return false;
        switch(strtoupper(substr($query, 0, 6))){
        case "SELECT" : $this->DB_RESULT_ROWS = @mysql_num_rows($this->DB_RESULT);                       
                        while ($db_result = mysql_fetch_array($this->DB_RESULT, MYSQL_ASSOC))
                        {
                            $db_data[] = $db_result;
                        }                       
                       if (is_array($db_data))
                       {
                         return $db_data;
                       }
                       else
                          return false;


                        break;

        case "INSERT" : $this->DB_AFFECTED_ROWS = @mysql_affected_rows($this->DB_RESULT);
                        $this->DB_PREV_INSERT_ID = @mysql_insert_id();
                        break;

        case "DELETE" : $this->DB_AFFECTED_ROWS = @mysql_affected_rows($this->DB_RESULT);
                        break;

        case "UPDATE" : $this->DB_AFFECTED_ROWS = @mysql_affected_rows($this->DB_RESULT);
                        break;
      }        

      return true;
    }

    function querySingle($query)
    {
      if (!$db_result = $this->query($query))
        return false;

      return $db_result[0];
    }       

    function clean()
    {
      if ((!$this->DB_CONNECTION) || (!$this->DB_RESULT))
        return false;

      if (!(@mysql_free_result($this->DB_RESULT)))
        return false;

      $this->DB_RESULT         = false;
      $this->DB_RESULT_ROWS    = 0;
      $this->DB_AFFECTED_ROWS  = 0;
      $this->DB_PREV_INSERT_ID = 0;

      return true;
    }

    function close()
    {
      if (!$this->DB_CONNECTION)
        return false;

      if (@mysql_close($this->DB_CONNECTION))
      {
        $this->DB_RESULT         = false;
        $this->DB_CONNECTION     = false;
        $this->DB_RESULT_ROWS    = 0;
        $this->DB_PREV_INSERT_ID = 0;

        return true;
      }

      return false;
    }   
  }

?>


class.exibir.php


<?php
    class Exibir
    {
        var $DB_RESULT_ROWS = 0;


        ########## Empresa ############

        function Sobre(){
            $db = new xDataBase();
            $strSql = "SELECT * FROM 'tb_sobre' WHERE id = 1";
            $result = $db->query($strSql);
            $this->DB_RESULT_ROWS = $db->DB_RESULT_ROWS;
            $db->close();
            if($this->DB_RESULT_ROWS != 0){
                return($result);
            }else{
                return false;
            }
        }
    }
?>


sobre.php

<?php  $B = $Exibir->Sobre(); if(is_array($B)){  foreach($B as $b) { ?>

<?php echo utf($b['Texto']); ?>

<?php  } } // Fecha Foreach
 else{ echo "Falha ao exibir resultados do banco de dados!!!";} // Fecha Else
?>
    
asked by anonymous 09.09.2018 / 04:54

3 answers

-1

Use PDO.

  

The use of the PDO provides an abstraction layer regarding the connection to the database since the PDO connects to several databases in the same way, modifying only its connection string.

Take a look at the PHP7 documentation and object-oriented programming techniques.

I, in particular, make the connection to the bank this way:

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'dbname');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_CHAR', 'utf8');

class Database{

    // specify your own database credentials

    private static $conn;

    // get the database connection
    public static function getConnection(){

        if(static::$conn == null){
                try{
                    $opt  = array(
                        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                        PDO::ATTR_EMULATE_PREPARES   => FALSE,
                    );
                    $dsn = 'mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset='.DB_CHAR;
                    static::$conn = new PDO($dsn, DB_USER, DB_PASS, $opt);
                }catch(PDOException $exception){
                    die("Connection error: " . $exception->getMessage());
                }
        }
        return static::$conn;
    }
}
?>
    
09.09.2018 / 06:01
-1

Well what I'd like to try to do is these functions that connects your stipulated tables to the query. I see that they stop working in PHP 7.1.

class.exhibit.php

<?php
    class Exibir
    {
        var $DB_RESULT_ROWS = 0;


        ########## Empresa ############

        function Sobre(){
            $db = new xDataBase();
            $strSql = "SELECT * FROM 'tb_sobre' WHERE id = 1";
            $result = $db->query($strSql);
            $this->DB_RESULT_ROWS = $db->DB_RESULT_ROWS;
            $db->close();
            if($this->DB_RESULT_ROWS != 0){
                return($result);
            }else{
                return false;
            }
        }

        function Contato(){
            $db = new xDataBase();
            $strSql = "SELECT * FROM 'tb_contato' WHERE id = 1";
            $result = $db->query($strSql);
            $this->DB_RESULT_ROWS = $db->DB_RESULT_ROWS;
            $db->close();
            if($this->DB_RESULT_ROWS != 0){
                return($result);
            }else{
                return false;
            }
        }
    }
?>
    
10.09.2018 / 20:50
-1

According to documentation of the mysql extension of PHP , these methods have been deprecated in PHP version 5.5 and removed in PHP 7.

The least intrusive way to change your code would be to use the mysqli extension. with the procedural interface, because it is closer to your current code. As stated in the documentation

  

Users migrating from the old mysql extension may prefer the procedural interface. The procedural interface is similar to that of the old mysql extension. In many cases, the function names differ only by prefix. Some mysqli functions take a connection handle as their first argument, whereas matching functions in the old mysql interface take it as an optional last argument.

Free translation:

  

Users who are migrating from the old version of the mysql extension may prefer the procedural interface. The procedural interface is similar to the old extension interface. In many cases, the function names differ only by the prefix. Some mysqli functions get a connection handle as the first argument, where the same function in the old extension, receives the handle as the last argument, being this optional. >

In other words, refactoring would be more like switching% w /% w /% w /% and care for cases where exceptions mentioned above occur. This response , as noted by the moderators, contains more information on how to do this refactoring.

    
10.09.2018 / 21:25