0

Estoy usando prepares statements para mejorar una consulta que estoy realizando, pero me genera el error:

Wrong number of parameters

El código es el siguiente:

$start = new DateTime('first monday of January 2016');
$end   = new DateTime('last day of December 2016');

$sql = "SELECT ".$generalLand."
                         product AS product,
                         Week AS label,
                         ROUND(SUM(harvest)/SUM(production),2) AS value
                  FROM (
                          (
                              SELECT ".$fieldLand."
                                     pr_products.product,
                                     CONCAT(YEAR(:dates),'-', LPAD(WEEK(:dates1),2,'0')) AS Week,
                                     SUM(IF(sw_sowing.type = 'SW', sw_sowing.quantity,0)) AS PlantSowing,
                                     SUM(IF(ROUND(DATEDIFF(TIMESTAMPADD(DAY,(6-WEEKDAY(:dates2)),:dates3), sw_sowing.date)/7) >= pr_products.week_production AND sw_sowing.type = 'SW',sw_sowing.quantity,0)) AS production,
                                     0 AS Harvest
                              FROM (
                                      SELECT max(sw_sowing.id) AS id
                                      FROM sw_sowing
                                      WHERE sw_sowing.status != 0
                                      AND sw_sowing.date <= TIMESTAMPADD(DAY,(6-WEEKDAY(:dates4)),:dates5)
                                      GROUP BY sw_sowing.id_production_unit_detail
                                   ) AS sw
                              INNER JOIN sw_sowing ON sw_sowing.id = sw.id
                              INNER JOIN pr_products ON pr_products.id = sw_sowing.id_product
                              INNER JOIN pr_varieties ON sw_sowing.id_variety = pr_varieties.id
                              ".$innerSowing."
                              WHERE pr_varieties.code != 1
                              AND sw_sowing.id_product = 1
                              AND sw_sowing.status = 100
                              AND sw_sowing.id_tenant = :id_tenant
                              ".$consultSowing."
                              GROUP BY pr_products.product
                              HAVING plantSowing > 0
                              ORDER BY pr_products.product
                          )
                          UNION ALL
                          (
                              SELECT  ".$fieldLand."
                                      pr_products.product,
                                      CONCAT(YEAR(:dates6),'-', LPAD(WEEK(:dates7),2,'0')) AS Week,
                                      0 AS plantSowing,
                                      0 AS Production,
                                      SUM(pf_harvest.quantity) AS Harvest
                              FROM pf_harvest
                              INNER JOIN pr_products ON pr_products.id = pf_harvest.id_product
                              INNER JOIN pr_varieties ON pr_varieties.id = pf_harvest.id_variety
                              INNER JOIN pf_performance ON pf_performance.id = pf_harvest.id_performance
                              ".$innerHarvest."
                              WHERE pf_harvest.date BETWEEN TIMESTAMPADD(DAY,(0-WEEKDAY(:dates8)),:dates9)
                              AND TIMESTAMPADD(DAY,(6-WEEKDAY(:dates10)),:dates11)
                              AND pr_varieties.code != 1
                              AND pf_harvest.id_product = 1
                              AND pf_performance.status = 100
                              ".$consultHarvest."
                              AND pf_harvest.id_tenant = :id_tenant1
                              GROUP BY pr_products.product
                              ORDER BY pr_products.product
                              )
                          ) AS sc
                  GROUP BY product, label
                  ORDER BY label";

        $statement = $this->db->prepare($sql);
        $id_tenant = $this->getIdTenant();
        foreach($datePeriod AS $dates){

          $values = [
            ':dates'      => $dates->format('Y-m-d'),
            ':dates1'     => $dates->format('Y-m-d'),
            ':dates2'     => $dates->format('Y-m-d'),
            ':dates3'     => $dates->format('Y-m-d'),
            ':dates4'     => $dates->format('Y-m-d'),
            ':dates5'     => $dates->format('Y-m-d'),
            ':dates6'     => $dates->format('Y-m-d'),
            ':dates7'     => $dates->format('Y-m-d'),
            ':dates8'     => $dates->format('Y-m-d'),
            ':dates9'     => $dates->format('Y-m-d'),
            ':dates10'    => $dates->format('Y-m-d'),
            ':dates11'    => $dates->format('Y-m-d'),
            ':id_tenant'  => $id_tenant,
            ':id_tenant1' => $id_tenant

          ];

          $result = $this->db->executePrepared($statement , $values);

        }

He estado mirando pero no he podido encontrar el error o el por qué del error, si alguien puede darme una sugerencia se lo agradecería.

2
  • en $consultSowing y $consultHarvest no tienes algun parametro :parametro ?
    – jolsalazar
    Commented el 8 feb. 2017 a las 3:02
  • No, ninguno son sólo campos de una tabla cuando se seleccione una opción Commented el 8 feb. 2017 a las 3:03

1 respuesta 1

0

Por lo que investigue,

En Phalcon, la funcion executedPrepared recibe 3 parametros.

public PDOStatement executePrepared (
            PDOStatement $statement, 
            array $placeholders, 
            array $dataTypes)

Espero esta sea la solución, me comentas.

https://docs.phalconphp.com/en/3.0.1/api/Phalcon_Db_Adapter_Pdo.html

4
  • Es que use los 3 parámetros y tampoco me funcionó, pero voy a revisar si es que hay lago mal! gracias ! Commented el 8 feb. 2017 a las 12:10
  • La verdad es lo que pude ver en tu código, ya que por el error el único que calzaba con el error era esa función. Si descubre el problema, comparte.
    – jolsalazar
    Commented el 8 feb. 2017 a las 13:06
  • Si @jolsalazar si funcionó era eso ! Muchas gracias! saludos! Commented el 9 feb. 2017 a las 1:23
  • genial!! =) saludos,
    – jolsalazar
    Commented el 9 feb. 2017 a las 3:10

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.