Function on API

$input = $request->getParsedBody();
$cups = $input['cups'];
$sqlfechas="SELECT fecha_inicio, fecha_fin XXX al, XXX c WHERE AND c.cups_electricidad = :cups ORDER BY fecha_facturacion DESC LIMIT 1";
$fecha1 = $todos->fecha_inicio;

if ($sth->rowCount()>0){

    function calcular_termino_electricidad($cups,$fecha_inicio,$fecha_fin){



I am trying to do an API call, and inside my call i need to have a function, but when i try to do the call to the url, i get this error

PHP Fatal error:  Call to undefined function calcular_termino_electricidad()

Is there any way my function works when i make the call?


If you define a function within a closure you have to define the function first. Just move the function declaration one line above the function call. Example:

function calcular_termino_electricidad($cups,$fecha_inicio,$fecha_fin)
   // ...


Now Im getting error of PHP Fatal error: Using $this when not in object context and im think it is because above my function i have $sth->execute() and inside my function I have this

$sth1 = $this->db->prepare($sql);
$todos1 = $sth1->fetchAll();

any way to by pass that?

I recommend switching to OOP and implementing controllers.

Please read here for more information.

I’m not 100% sure I understand but if you’re function is defined before or outside of your closure, you can probably pass “$this” as an argument, right? Just a guess on my part. YMMV.

I suggest you to dump $this so you can really understand what $this is inside closure,
than take your direction.
you don’t need to move your function inside closure
best practice

  1. use the container

  2. use “use()” statement on closure to pass function.

So I changed to this. $rfechas=mysqli_query($sqlfechas); and I am getting a response from my code, but getting this errors
PHP Warning: mysqli_query() expects at least 2 parameters, 1 given and

PHP Warning:  mysqli_fetch_row() expects parameter 1 to be mysqli_result, null given

My code is this.

$sqlfechas="some sql";

if (mysqli_num_rows($rfechas)>0){

    if (is_array($electricidad)){
        foreach ($electricidad as $periodo) {
            # code...
            foreach ($periodo as $id_periodo => $fechas) {
                # code...
                foreach ($fechas as $fecha => $datos) {
                    # code...
                    $consumoTotal = $consumoTotal + $datos['consumo'];

        echo json_encode(array('error'=>0, 'lectura'=>$consumoTotal,'fecha_ini'=>$fecha1,'fecha_fin'=>$fecha2));
 echo json_encode(array('error'=>0, 'lectura'=>$consumoTotal,'fecha_ini'=>$fecha1,'fecha_fin'=>$fecha2));
    echo json_encode(array('error'=>1));

and i am getting error 1, question is now (I know nothing about PHP haha) i am getting echo error 1 because my query doesnt find any data, or because i am not given the mysqli_connect params to mysqli_query and mysqli_fetch_rows?

You have wrong to call mysqli_query function. It should be mysqli_query($param1,$param2);

I never use mysqli before. I just take a look on the error message.