flightphp / core

An extensible micro-framework for PHP
https://docs.flightphp.com
MIT License
2.6k stars 407 forks source link

How to use Medoo framework object #412

Closed rajkumarpb closed 4 years ago

rajkumarpb commented 4 years ago

Hi @mikecao

As per #348 , I have registered the Medoo framework class with flightphp. But not able to get any data and I'm getting 500 error. Can you please tell me whats wrong with the code?

Flight::route('GET /getclients', function(){
    $conn = Flight::db();   
    $data = $conn->select("client", [
        "[>]state" => ["client_state" => "state_id"],
        "[>]country" => ["client_country" => "country_id"],
    ]);
    echo json_encode($data);
});
magikstm commented 4 years ago

There's not enough in your code to determine the issue.

I would suggest: 1) Try routing without database connection 2) Try reviewing PHP error logs for hints 3) Use xdebug to debug

If these fail, please share more code and ideally a link to your code online.

My guess is that it is a database init/connection issue.

rajkumarpb commented 4 years ago

Full Code here: Before using Medoo I was using PDO and it was working fine. Only with Medoo I'm getting 500 Internal Server Error.

<?php
require 'vendor/autoload.php';
require  'Medoo.php';

// Using Medoo namespace
use Medoo\Medoo;

$medoo_Config = [
    // required
    'database_type' => 'mysql',
    'database_name' => 'xxxx',
    'server' => 'localhost',
    'username' => 'yyyy',
    'password' => 'zzzz',
    // Enable logging
    "logging" => true,
];

Flight::register( "db", new Medoo( $medoo_Config ), [ $medoo_Config ] );

/* Flight::register('db', 'PDO', array('mysql:host=localhost;port=3306;dbname=xxxxx', 'yyyy', 'zzzz'), function($db) {
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}); */

Flight::route('GET /getclients', function(){
    $conn = Flight::db();
    /*$conn = Flight::db();
    $idata = [];
    $SQL = "SELECT c.*, s.state_name,s.state_id, cn.country_name FROM client c, state s, country cn WHERE c.client_state = s.state_id AND c.client_country = cn.country_id";
    $dbdata = $conn->query($SQL);
    if(!empty($dbdata)) {
        foreach($dbdata as $row) {
            array_push($idata, $row);
        }
    }

    echo json_encode($idata);*/
    $data = $conn->select("client" , [
        "[>]state" => ["client_state" => "state_id"],
        "[>]country" => ["client_country" => "country_id"],
    ],[]);
    echo json_encode($data);
});

Flight::start();
?>
keskinonur commented 6 months ago

If someone needed. Line 19 has to be like this:

Flight::register("db", 'Medoo\Medoo', [ $medoo_Config ]);

n0nag0n commented 6 months ago

FWIW, I'm putting this in the framework in the next little bit. Just a simplified Pdo_Wrapper https://github.com/flightphp/core/compare/master...database-class