0

Async Mysql Client of Swoole in my PHP Project for which I am trying to use Swoole MySQL Class of PHP but I am getting

class not found

error while executing the code.

Below is the code snippet I was trying to execute:

<?php

use Swoole\MySQL as MySQL;

MySQL::connect(array(
    'host'     => 'localhost',
    'port'     => '3306',
    'user'     => 'root',
    'password' => 'Secret_Password',
    'database' => 'swoole_test_db',
    'charset'  => 'utf8',
    'timeout'  => 2,
), function ($mysql_client) {
    $mysql_client->query("SELECT `reg_id`, `name` FROM `students` WHERE `reg_id` = 8;", function ($result) {
        var_dump($result);
    });
});

I tried searching everywhere but unable to find any reference on this error.

Version Details

  • PHP 7.3.2
  • Swoole Version 4.4.3 (with Swoole MySQL selected Yes during installation)

Swoole info from phpinfo()

Did I miss something, please help?

Thanks in advance!!!

Akshay Khale
  • 8,151
  • 8
  • 50
  • 58
  • 1
    Did you enable the extension from php.ini file? https://github.com/swoole/swoole-docs/blob/master/get-started/installation.md – catcon Aug 07 '19 at 05:42
  • Yes, Swoole HTTP Server is working fine for me @catcon – Akshay Khale Aug 07 '19 at 05:43
  • If you have installed it via composer make sure to `require_once 'vendor/autoload.php';` . I don't see that in your code – BlackXero Aug 07 '19 at 06:15
  • Swoole is installed as a PHP Extension and not using composer. All the classes are available out of the box just like SoapClient or like mysqli @BlackXero – Akshay Khale Aug 07 '19 at 06:26
  • if you don't use autoloader you need to require `.php` file before your namespace – nima Aug 07 '19 at 06:28
  • Nope, it's not required @nima – Akshay Khale Aug 07 '19 at 06:30
  • Ok than you should `require_once()` the file that you `use Swoole\MySQL` or use autoloader like `composer` to do all thing for you – nima Aug 07 '19 at 06:36
  • 1
    @nima you don't know what you are talking about :) @Akshay Kale is this extension really enabled? Please run `phpinfo();` and check it's really there. – Mike Doe Aug 07 '19 at 06:43
  • @emix I have added the `phpinfo();` screenshot of Swoole section. – Akshay Khale Aug 07 '19 at 06:49
  • Have you tried using the [official documentation](https://github.com/swoole/swoole-src)? – Mike Doe Aug 07 '19 at 06:50
  • Yes, @emix, I followed the official documentation and code on following URL: https://www.swoole.co.uk/docs/modules/swoole-async-mysql-client which is also not working for me `Class 'swoole_mysql' not found` – Akshay Khale Aug 07 '19 at 06:52
  • Can you list Swoole related classes by running `print_r(get_declared_classes())` – Mike Doe Aug 07 '19 at 06:53
  • I listed all the classes and learned that there is no `Swoole\MySQL` but there are following classes related to MySQL `Swoole\Coroutine\MySQL`, `Swoole\Coroutine\MySQL\Statement` and `Swoole\Coroutine\MySQL\Exception` are we supposed to use these @emix – Akshay Khale Aug 07 '19 at 06:58
  • 1
    The github link I posted earlier mentions `Swoole\Coroutine\MySQL`. The official PHP docs seems to be a bit off. Maybe they mention previous versions of Swoole. – Mike Doe Aug 07 '19 at 06:59
  • sure, thank you for your help, please post an Answer so that I can accept it and close it @emix – Akshay Khale Aug 07 '19 at 07:02

1 Answers1

1

It seems that the official documentation is either a bit off, or mentions a previous version of Swoole.

The class that should be used is Swoole\Coroutine\MySQL.

You can verify that using the official documentation in their GitHub repository.

Mike Doe
  • 16,349
  • 11
  • 65
  • 88