Exchange PostgreSQL connection for MySQL connection

2

I found a project made in PHP OO to be used in PostgreSQL with this connection file.

   <?php
   class BD {
       public function __construct() {
           pg_connect("host=localhost port=5432 user=usuario password=senha dbname=nome_do_BD")
            or die("Erro ao conectar ao servidor");
   }
   public function __destruct() {
    //pg_close();
   }
   }
   ?>

How can I change this file to access DB in phpMyAdmin?

I'm using WampServer.

I tried this, but to no avail:

   <?php
      class BD {
         public function __construct() {
    pg_connect("host='localhost' user='root' password='' dbname='nome_do_BD'")
            or die("Erro ao conectar ao servidor");
    }
         public function __destruct() {
    //pg_close();
    }
    }
    ?>
    
asked by anonymous 06.09.2015 / 14:33

3 answers

2

I think phpMyAdmin has nothing to do with it.

Certainly you can change the access of PostgreSQL to MySQL but will have to change in several places. The change is not so simple.

The first change is obviously the connection, do not just change the string connection, you have to change the library. The change begins by using mysqli_connect()

$conexao = mysqli_connect("localhost", "usuario", "senha", "banco");

Note that this variable $conexao must be stored somewhere in the class. The original code he was using was not even something that made sense, he would open the connection to abandon her. So it does not look like a useful code and it will help you in something. I would look for a better source.

Another obvious change is to change the lock. In MySQL it is mysqli_close($conexao); .

But I repeat, unless you are putting insufficient code, this class using PostgreSQL was done very naively, do not take advantage of it.

The mysqli library already has an object-oriented form, if you prefer this style of programming. Take a look at it . Or look for a class that encapsulates all this functionality in a better way. Do not try to reinvent the wheel. In fact if you do not have a good reason to create this whole complication, do not do it.

    
06.09.2015 / 14:44
0

Only a few connection parameters using postgree and mysql change, for example; instead of putting pg_connect use mysql_connect , since you are using local server (mysql) there is no need to inform the port, another difference in the mysql connection is that within the connection information you do not need to inform " which is what, "for example; instead of:

 pg_connect("host='localhost' user='root' password='' dbname='nome_do_BD'")
        or die("Erro ao conectar ao servidor");

use:

mysql_connect('localhost', 'mysql_user', 'mysql_password');

You can also opt to use the connection with mysqli (with i at the very end!)

mysqli_connect("localhost","my_user","my_password","my_db");

Notice that in connection mysql and mysqli you do not need to tell what is host and what is user as in postgree , just enter the data in the correct order that everything will work

I suggest you use mysqli or even better, use pdo , all the documentation you can find in official php documentation

And last but not least ... to close the connection use mysql_close()

    
06.09.2015 / 14:55
-1

Do the following:

$serverIp = "Localhost";
$UserServer ="Root";
$PasswordServer = "Password do respectivo user";
mysql_connect($serverIp,$UserServer,$PasswordServer); 
or die(mysql_error); //Este die(mysql_error); retorna uma mensagem de erro se 
// encaso correr alguma coisa mal 

View this link

or

This

    
24.05.2016 / 21:42