Exchange Postgresql connection for Mysql connection

Asked

Viewed 451 times

2

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

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

Is there any way I can change this file to access the BD in phpMyAdmin?

I’m using the Wampserver.

I tried that, 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();
    }
    }
    ?>
  • This link: [TUTORIAL] Connect PHP site to MYSQL database teaches how to make a connection to mysql! - Feather Painted now edit

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

2

I believe phpMyAdmin has nothing to do with it.

Certainly you can change the access from Postgresql to Mysql but will have to change in several places. The change is not so simple.

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

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

Note that this variable $conexao It has to be stored somewhere in the class. The original code he was using wasn’t even something he felt, it opened the connection and left it. So it doesn’t seem like useful code that will help you with anything. 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 made in a very naive way, do not enjoy anything of it.

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

0

Only some connection parameters using postgree and mysql change, for example; rather than putting pg_connectuse mysql_connect, as you are using local server (mysql) there is no need to inform the port, another difference in connection mysql is that within the connection information you do not need to inform "what 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 choose can use the connection with mysqli(with i at the very end!)

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

realize that in connection mysql and mysqli you don’t need to inform what is host nor 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 on official php documentation

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

-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 

See this link

or

This

Browser other questions tagged

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