Translation of MySQL code for PostgreSQL [closed]

7

I need to convert a MySQL page into PostgreSQL commands, but I have already reviewed the internet looking for similar commands and PostgreSQL syntax, and I did not succeed.

Some commands do not exist for PostgreSQL and are declared together with others, and so on.

If anyone can help, it would be wonderful for me.

Code:

<?php
//utilização de namespaces
namespace Mysql {
    //declaração de variáres globais
    define('DB_SERVER', 'localhost');
    define('DB_NAME', 'acesso');
    define('DB_USERNAME', 'root');
    define('DB_PASSWORD', '');

    class mysql {
        var $db, $conn;
        public function __construct($server, $database, $username, $password) {
            $this->conn = mysql_connect($server, $username, $password);
            $this->db = mysql_select_db($database, $this->conn);
        }
        /**
         * Função de seleção dos registros da tabela
         * @param string $tabela tabela onde será buscado os registros
         * @param string $colunas string contendo as colunas separadas
por virgula para seleção, se null busca por todas *
         */
        public function select($tabela, $colunas = "*", $where = "1=1") {
            $sql = "SELECT $colunas FROM $tabela $where";
            $result = $this->executar($sql);
            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $return[] = $row;
            }
            return $return;
        }

        /**
         * Função para inserir dados na tabela
         * @param array $dados Array contendo os dados a serem inseridos
         * @param string $tabela tabela que será inserido os dados
         * @return boolean verdadeiro ou falso
         */
        public function insert($tabela, $dados) {

            foreach ($dados as $key => $value) {
                $keys[] = $key;
                $insertvalues[] = '\'' . $value . '\'';
            }
            $keys = implode(',', $keys);
            $insertvalues = implode(',', $insertvalues);
            $sql = "INSERT INTO $tabela ($keys) VALUES ($insertvalues)";
            return $this->executar($sql);
        }
        private function executar($sql) {
            $return_result = mysql_query($sql, $this->conn);
            if ($return_result) {
                return $return_result;
            } else {
                $this->sql_error($sql);
            }
        }
        private function sql_error($sql) {
            echo mysql_error($this->conn) . '<br>';
            die('error: ' . $sql);
        }
    }
}
?>

Can I freely edit and replace this namespace MySQL with this namespace PostgreSQL? Or is it a MySQL function?

In the mysql_select_db: part of what would the PostgreSQL language look like? I did not find equivalence.

    
asked by anonymous 17.11.2015 / 17:27

1 answer

-1

In addition to the PDO mentioned by the colleague, I will also recommend as an option an ORM framework such as TORM or Doctrine .

All these abstract the part of the database making the same code compatible with any bank without major changes.

    
17.11.2015 / 18:11