Tiago Pires
Tiago Pires

Reputation: 21

Call Mysql function from Slim Framework

I'm trying to call a function I created in MySQL using the Slim framework. This is my function in DBHandler.php:

public function validarSincronismo($pCnpj, $pLogin, $pImei){
    $stmt = $this->conn->prepare("SELECT sincronizar(?,?,?)");
    $stmt->bind_param("sss", $pCnpj, $pLogin, $pImei);
    $result = $stmt->execute();
    $stmt->close();
    return $result;
}

And this is the function in my index.php:

$app->post('/validar', function() use ($app) {
    $db = new DbHandler();
    $cnpj =    $app->request->post('cnpj');    
    $login = $app->request->post('login');
    $imei = $app->request->post('imei');
    $msg = $db->validarSincronismo($cnpj, $login, $imei);                 
    $response["error"] = false;
    $response["message"] = $msg;
    echoRespnse(201, $response);
});

And I'm getting the following error in phperror.log:

[17-Sep-2015 21:12:37 UTC] PHP Fatal error:  Call to a member function execute() on boolean in C:\MAMP\htdocs\test\include\DbHandler.php on line 69

I tried using CALL sincronizar(?,?,?); But it doesn't execute the SQL function.

Upvotes: 1

Views: 1394

Answers (1)

Tiago Pires
Tiago Pires

Reputation: 21

Thanks @GustavoStraube and @NorbertvanNobelen for taking the time and looking into my question! I was able to call my SQL function using SELECT sincronizar(). The problem was that I had created the function in the wrong database. My bad! :/

So my final and working code looks as follows:

Function in DBHandler.php

public function validarSincronismo($pCnpj, $pLogin, $pImei){
    $stmt = $this->conn->prepare("SELECT sincronizar(?,?,?)");
    $stmt->bind_param("sss", $pCnpj, $pLogin, $pImei);
    $stmt->execute();
    $stmt->bind_result($result);
    $stmt->fetch();
    $stmt->close();
    // Returns a message
    return $result;
}   

Function in index.php

$app->post('/validar', function() use ($app) {
            $db = new DbHandler();
            $cnpj = $app->request->post('cnpj');    
            $login = $app->request->post('login');
            $imei = $app->request->post('imei');
            $msg = $db->validarSincronismo($cnpj, $login, $imei);                 
            $response["error"] = false;
            $response["message"] = $msg;
            echoResponse(201, $response);
        });

Upvotes: 1

Related Questions