Mysql Code Translation for Postgresql

Asked

Viewed 693 times

7

I need to convert a Mysql page into Postgresql commands, but I’ve already reviewed the internet looking for similar commands and Postgresql syntax, and was unsuccessful.

Some commands do not exist for Postgresql and are declared along 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);
        }
    }
}
?>

I can freely edit and replace this namespace Mysql by that namespace Postgresql? Or is it a Mysql function?

On the part of mysql_select_db: how would it look in the language of Postgresql? I did not find the equivalence.

  • 2

    Just switch to the pg_* functions, follow the command list. If you have any specific questions edit the question :). You can use PDO as well, it is for Mysql and Postgressql.

  • 5

    The way the question looks has no specific doubt, asks the rewriting of the code.

  • @rray is what I’m trying to do. But as I said, I’m very lay and it’s kind of hard for me, because it’s not just about changing where mysql is written to pg. Some functions do not exist in postgres as they are done otherwise. When the PDO, I am informing myself about. I have never heard of.

  • In place of mysql_select_db() use only the pg_connect() the difference is that you already inform the database, see an example: $con = pg_connect("host=localhost dbname=nome_do_banco user=usuario password=senha");

  • I suggest you start by doing a test, create a separate file, with the postgres connection, then make a query using the pg_query() and get the result with the pg_fetch_*() when it is working and a little more familiar with the functions start to change their class. If you need to create another question with the code and the error message. The first step is sometimes to make a mistake anyway, so fix the problem and go ahead.

1 answer

-1


Beyond the PDO cited by the colleague, I will also recommend as an option an ORM framework as TORM or Doctrine.

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

  • I couldn’t use any of these. I don’t think I understood the essence. I keep trying to edit the code for the postgresql language.

  • The most similar to your model is PDO that uses the same "essence" of your code style. ORM frameworks use a different "essence". Since you will have to reformulate all your code, I believe it pays to use the PDO. In addition to this quoted, has several tutorials on the internet.

Browser other questions tagged

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