How to create the same codes to run in php7?

Asked

Viewed 107 times

-3

Good people I am a little behind in things and so far I could not learn php7 again.

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

Or if there is something more current and easier to be implementing 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
?>

3 answers

-1

Use PDO.

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

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

I particularly 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;
    }
}
?>
  • Well I believe my problem was not about connection in Pdo but rather about a class file with functions that connect to a different table!

-1

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

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

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

-1

According to the extension documentation mysql of PHP, these methods were discontinued in PHP 5.5 and removed in PHP 7.

The least intrusive way to change your code would be to use the extension mysqli with the procedural interface as it is closer to your current code. As stated in 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 interface of the old extension. In many cases, the function names differ only by prefix. Some functions mysqli receive a Handle connection as first argument, where the same function in the old extension, receives the Handle as a last argument, this being optional.

That is, refactoring would be more about changing mysql_* for mysqli_* and take care of cases where the above mentioned exceptions occur. This answer, as marked by moderators, contains more information on how to do this refactoring.

  • Any hint of what’s wrong with the answer, or it’s just -1 out of the blue?

  • 2

    I don’t understand! The reply of the fernandosavio was the best because it explains where the error is and the guy won a -1 :(

  • Apart from the part that suggests the PDO in the last paragraph, the rest is good, I see no reason for -1 in this one. Maybe an example of code would help a little.

  • It wasn’t exactly a suggestion to use PDO, but rather to use PDO, mysqli or ORM. It wasn’t in order of importance. Anyway I will edit and reply because that part does not add much to the same answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.