Search code examples
phpmysqliparametersbindparam

mysqli_query() expects parameter 2 to be string with bind_param


i am getting the error when i call the archive.php in browser: Warning: mysqli_query() expects parameter 2 to be string, object given in...

<?php
    $connection = new mysqli('localhost','root','', 'scale');

    if (mysqli_connect_errno()) {
        printf("Error de conexión: %s\n", mysqli_connect_error());
        exit();
    }

    $stmt = $connection->prepare('  SELECT
                                    R.cod_region, P.region, P.comuna, COUNT(DISTINCT(P.id)) AS sitios, COUNT(DISTINCT(A.TEXTO)) AS alarmas
                                    FROM region R, pop P, sitios_pop SP, sitios S
                                    LEFT JOIN log_alarm_2g A ON S.RSITE = A.RSITE AND A.CLASE = "A1"
                                    AND A.INICIO >= "2016-02-14"
                                    WHERE
                                    R.cod_region = ? AND
                                    R.region = P.region AND
                                    S.ESTADO = "OPERATIVO" AND
                                    S.SITIO = SP.cod_sitio AND
                                    SP.id_pop = P.id
                                    GROUP BY
                                    P.region,
                                    P.comuna
                                    ORDER BY
                                    R.cod_region ');

    $stmt->bind_param('i', $cod_region);

    $resultset = mysqli_query($connection, $stmt);
    $records= array();
    while($r = mysqli_fetch_assoc($resultset)){
        $records[] = $r;
    }       
    echo json_encode($records);
?>

Solution

  • For using prepare and bind statement we use execute() instead of mysqli_query()

    What you have to do use execute() and get_result() as

      $stmt->bind_param('i', $cod_region);
    
      // $resultset = mysqli_query($connection, $stmt);
      $stmt->execute();// use execute
      $records= array();
    
      $result = $stmt->get_result();
       while ($data = $result->fetch_assoc($result))
      {
         $records[] = $data;
      }