friends-of-reactphp / mysql

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

Can`t get any result from query. #113

Closed Nereg closed 5 years ago

Nereg commented 5 years ago

Hello ! I`m experiencing some problems with that code :

<?php
require_once "../vendor/autoload.php";
$user = '***********';
$pass = '***********';

$loop = \React\EventLoop\Factory::create();
$factory = new \React\MySQL\Factory($loop);

$connection = $factory->createLazyConnection(
    rawurlencode($user) . ':' . rawurlencode($pass) . '@localhost:3306/admin_default'
);
$timer = $loop->addPeriodicTimer(0.1, function () use($connection){
$query = "SELECT * FROM settings";
$connection->query($query)->then(function (QueryResult $command) {
    echo "query"; 
    if (isset($command->resultRows)) {
        // this is a response to a SELECT etc. with some rows (0+)
        print_r($command->resultFields);
        print_r($command->resultRows);
        echo count($command->resultRows) . ' row(s) in set' . PHP_EOL;
    } else {
        // this is an OK message in response to an UPDATE etc.
        if ($command->insertId !== 0) {
            var_dump('last insert ID', $command->insertId);
        }
        echo 'Query OK, ' . $command->affectedRows . ' row(s) affected' . PHP_EOL;
    }
}, function (Exception $error) {
    // the query was not executed successfully
    echo 'Error: ' . $error->getMessage() . PHP_EOL;
});
});
    echo "Allive";
$loop->run();
?>

I just can't get any result from this query. Also I found that the request is being executed. What I'm doing wrong ?

clue commented 5 years ago

Hi @Nereg, good question!

It looks you're missing some imports for QueryResult. You have to import React\MySQL\QueryResult.

Because this import is missing, your code will yield a TypeError that will cause the resulting promise to be rejected. You should be able to see this problem by appending another ->then(null, 'printf') after the first then(). I understand this default behavior is frustrating, that's why we're working on https://github.com/reactphp/promise/issues/87 to improve default error reporting.

I hope this helps :+1:

Nereg commented 5 years ago

Yes that worked for me ! Thank you very much for your help !