mysqli_query()期望参数2是bind_param的字符串

时间:2016-05-28 05:00:19

标签: php mysqli parameters bindparam

我在浏览器中调用archive.php时收到错误: 警告:mysqli_query()期望参数2是字符串,对象在...中给出

<?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);
?>

1 个答案:

答案 0 :(得分:0)

要使用prepare and bind语句,我们使用 execute() 代替mysqli_query()

您必须使用execute()get_result()作为

  $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;
  }