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.
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.
– rray
The way the question looks has no specific doubt, asks the rewriting of the code.
– rray
@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.
– Léo Eduardo Silva
In place of
mysql_select_db()
use only thepg_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");
– rray
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 thepg_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.– rray