Search code examples
phpmysqlsqlmedoo

table joining using medoo returning false instead of the data


I'm trying to join two tables using MEDOO, the problem Im facing is that the query I'm building is not returning any data (it's returning false). I've done the query following this normal SQL code

select posts.id, title, extract, img_principal, users.username, date, category, platform, url from posts, users where users.username in (select username from users where users.id = posts.author);

and the result is this one:

    $lastPost = 10;

    $database = new medoo([
        'database_type' => 'mysql',
        'database_name' => 'notijuegosdb',
        'server' => 'localhost',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8'
    ]);


    $database->select("posts", [

        "[>]users" => ["author" => "users.id"]
    ], [
      'posts.id',
      'posts.title',
      'posts.extract',
      'posts.img_principal',
      'users.username',
      'posts.date',
      'posts.category',
      'posts.platform',
      'posts.url'
    ], [
        'posts.id[<]'=> $lastPost
    ]);

The SQL code Works perfectly when I use it directly to the DB so it might be a problem of my way of building the query... Not sure what could be wrong.

Thanks a lot in advance.


Solution

  • You could archive this using query medoo method used for complex querys:

    $postsData = $database->query("select posts.id, title, extract, img_principal, users.username, date, category, platform, url from posts, users where users.username in (select username from users where users.id = posts.author);")->fetchAll();
    

    Like Medoo doc says:

    This function is for special and customized SQL query that used for complex query. With each data that will be inserted, please use quote function to prevent SQL injection.

    Medoo API