friends-of-reactphp / mysql

Async MySQL database client for ReactPHP.
MIT License
331 stars 66 forks source link

Cant connect to MySQL using CLI #154

Closed fx1234 closed 2 years ago

fx1234 commented 2 years ago

Hi

I'm trying to use async mysql from the command line (cli)

to test it, i have this simple script (thats all, nothing more in it):

use React\MySQL\Factory;
use React\MySQL\QueryResult;

require "/composer/vendor/autoload.php";

$mysql_host = 'xxx.xxx.xxx.xxx';
$mysql_user = 'websocket';
$mysql_pass = 'xxxxxxxxxxxxxx';
$mysql_db   = 'test';

$mysql_url  = "" . $mysql_user . ":" . $mysql_pass . "@" . $mysql_host . "/" . $mysql_db . "";

$mysql_factory = new React\MySQL\Factory( );

$mysql_factory->createConnection($mysql_url)->then(function (ConnectionInterface $connection) {

   $connection->query("SELECT 123 FROM test LIMIT 1")->then(

        function (QueryResult $command) {

        print "Query done";

        },

        function (Exception $error) {

        print "Error";

        }

    );

   $connection->quit();

});

when I start it ('php test.php') nothing happens. The script runs continuously but I get no output. shouldn't 'query done' or 'error' be displayed on the command line? I can see on my database server that a connection has been established

WyriHaximus commented 2 years ago

Add ->done() at the end of the promise chain as you're now swallowing any errors. Not returning the query promise will also swallow any errors coming up in there.

SimonFrings commented 2 years ago

@fx1234 It seems like you are using ConnectionInterface $connection without an import for ConnectionInterface. I think this is why nothing happens when you start your application.

Hope this helps :+1:

fx1234 commented 2 years ago

hi guys

thanks for your answers!! the ->done() actually worked.

im new to this and it seems that i still have a few basic problems understanding the async concept :/

I have a websocket server (ratchet) that uses reactphp's event loop. I'm trying to get mysql to work there now

my code looks like this:

class MyApp implements MessageComponentInterface{

    public function __construct( $loop ) {

     /** **mysql here??** **/   

   }

   public function onOpen(ConnectionInterface $conn) {

    /** **mysql here??** **/    

   }

  /** onMessage() ... on Close() .... **/

}

$loop           = \React\EventLoop\Loop::get();
$app            = new MyApp( $loop );
$ws_server      = new \Ratchet\WebSocket\WsServer( $app );
$http_server        = new \Ratchet\Http\HttpServer( $ws_server );

/** **mysql here??** **/    

$secure_websockets = new \React\Socket\Server('0.0.0.0:443', $loop);

$secure_websockets = new \React\Socket\SecureServer($secure_websockets, $loop, 
    [
    'local_cert'    => 'cert.pem',
    'local_pk'      => 'privkey.pem',
    'verify_peer'   => false
    ]
);

$secure_websockets_server = new \Ratchet\Server\IoServer($http_server, $secure_websockets, $loop);

$loop->run();

this works great. now when a user connects i need a mysql connection in onOpen() of MyApp. what is the correct way to start a mysql connection here?
do i put the code (new React\MySQL\Factory( ); ...) in the constructor of MyApp or in onOpen() or outside of MyApp?

clue commented 2 years ago

thanks for your answers!! the ->done() actually worked.

im new to this and it seems that i still have a few basic problems understanding the async concept :/

@fx1234 Don't worry, getting used to async can take some time until it "clicks". Glad to hear this already helps you in either case!

[…] what is the correct way to start a mysql connection here? do i put the code (new React\MySQL\Factory( ); ...) in the constructor of MyApp or in onOpen() or outside of MyApp?

This is a bit hard to answer as part of this issue tracker as it's somewhat out of scope for this project. As a starting point, I would recommend taking a look at how this can be integrated in a web application like this: https://framework-x.org/docs/integrations/database/#recommended-class-structure. Once you understand the concept of dependency injection (DI), you can apply the same idea to your Ratchet application 👍

I hope this helps! :+1: