The Slim Framework support forum has moved to This Tender forum is no longer maintained or monitored.

Using Slim Framework

Sundar Machamasi's Avatar

Sundar Machamasi

07 Sep, 2015 07:34 AM

I need help in slim framework

  1. 1 Posted by Sundar Machamas... on 07 Sep, 2015 07:36 AM

    Sundar Machamasi's Avatar

    Hi I am sundar,
    I need to use json_encode to display result. Can u help me out?

  2. Support Staff 2 Posted by Andrew Smith on 10 Sep, 2015 08:24 AM

    Andrew Smith's Avatar

    You have not posted what you need help with, can you post some code so we can help where possible?

  3. 3 Posted by Sundar Machamas... on 10 Sep, 2015 08:27 AM

    Sundar Machamasi's Avatar

    *i need to change json format : my code is like this *
    require_once '../Slim/Slim.php';
    require_once '../db.php';

    $app = new Slim();

    $app->get('/users', 'getUsers');


    // Show All Users
    function getUsers(){
    $sql = "SELECT * FROM tbl_user ORDER BY user_id";
    $db = getConnection();
    $stmt = $db->query($sql);
    $users = $stmt->fetchAll(PDO::FETCH_OBJ);
    $db = null;
    echo '{"AllUsers":'. json_encode($users).'}';
    }catch(PDOException $e){
    echo '{"error":'. $e->getMessage().'}}';

  4. Support Staff 4 Posted by Andrew Smith on 10 Sep, 2015 08:46 AM

    Andrew Smith's Avatar

    But this has no relation to Slim itself, you are basically writing the code incorrectly.

    echo json_encode(array('AllUsers' => $users));

    That is what you need, you should probably take a look at a few tutorials online about creating json from an array.

  5. 5 Posted by Sundar Machamas... on 10 Sep, 2015 10:58 AM

    Sundar Machamasi's Avatar

    i need to add user info by passing parameters through url : Plz help me out
    of this
    here is my code:
    require_once '../Slim/Slim.php';
    require_once '../db.php';

    $app = new Slim();

    $app->post('/create', 'addUser'); // Add user


    // Add User
    function addUser() {
    error_log('addUser\n', 3, '/var/tmp/php.log');
    $request = Slim::getInstance()->request();
    $user = json_decode($request->getBody());
    $sql = "INSERT INTO tbl_user (username, password, address, phone) VALUES
    (:username, :password, :address, :phone)";
    try {
    $db = getConnection();
    $stmt = $db->prepare($sql);
    $stmt->bindParam("username", $user->username);
    $stmt->bindParam("password", $user->password);
    $stmt->bindParam("address", $user->address);
    $stmt->bindParam("phone", $user->phone);
    $user->user_id = $db->lastInsertId();
    $db = null;
    echo json_encode($user);
    } catch(PDOException $e) {
    error_log($e->getMessage(), 3, '/var/tmp/php.log');
    echo '{"error":{"text":'. $e->getMessage() .'}}';

    function getConnection(){
    // For Local Server
    $dbh = new PDO("mysql:host=$dbhost; dbname=$dbname", $dbuser, $dbpass);
    return $dbh;


    404 Page Not Found

    The page you are looking for could not be found. Check the address bar to
    ensure your URL is spelled correctly. If all else fails, you can visit our
    home page at the link below.

  6. 6 Posted by Sundar Machamas... on 10 Sep, 2015 11:04 AM

    Sundar Machamasi's Avatar

    i need to show different JSON format using slim framework . plz sombody post example link related to that.

  7. Josh Lockhart closed this discussion on 12 Feb, 2021 07:42 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac