SQL查询获得结果时,Prepared-statement无法正常工作

时间:2016-04-08 10:13:40

标签: php mysql mysqli prepared-statement

我已经提出了这个问题,我不清楚它无法正常工作的原因。

基本上我有这个查询,我加入两个不同的表寻找精确的信息,很长但我认为这不是问题(但我会发布它以防万一):

SELECT * FROM (
  SELECT p.idplan, p.idautor,p.nombre,p.descripcion,p.privacidad,p.modified,a.nombre AS autor,
    IF(p.idautor IN (SELECT idamigo FROM pintiplan_amigos as am WHERE p.idautor=idamigo and am.idautor= ? ), 2, 
      IF (p.idautor = ? ,1, 3)) AS flagRelacion
  FROM pintiplan_planes AS p 
  LEFT JOIN pintiplan_autores AS a
    ON p.idautor=a.idautor
  HAVING ((p.privacidad=0) OR (p.idautor=?))  
    AND p.idplan IN (SELECT idplan FROM pintiplan_planes_sitios GROUP BY idplan HAVING count(1) > 1)
    AND (
      UPPER(p.nombre) LIKE UPPER (CONCAT('%',?,'%')) OR 
      UPPER(p.descripcion) LIKE UPPER (CONCAT('%',?,'%')))
) as plan
JOIN (
  SELECT s.idsitio,s.latitud,s.longitud,s.imagen,ps.idplan,ps.idautor,
  MIN(ACOS (
    SIN(RADIANS(?)) * 
    SIN(RADIANS(s.latitud))+ 
    COS(RADIANS(?)) * 
    COS(RADIANS(s.latitud))* 
    COS(RADIANS(?-s.longitud)))*6378.137) AS distancia
  FROM pintiplan_planes_sitios AS ps 
  RIGHT JOIN pintiplan_sitios AS s 
    ON (ps.idautor=s.idautor) AND (ps.idsitio=s.idsitio) 
  GROUP BY ps.idplan,ps.idautor
) as sitio 
ON sitio.idplan=plan.idplan AND sitio.idautor=plan.idautor 
HAVING sitio.distancia < ?
LIMIT ?,?

我的问题在这里我认为是我的php:

if (checkPostParameters(array("cadena","radio","idautor","lat","lon","pag"))){
  $cadena = $_POST["cadena"];
  $radio = $_POST["radio"];
  $idautor = $_POST["idautor"];
  $lat = $_POST["lat"];
  $lon = $_POST["lon"];
  $pag = $_POST["pag"];
} else reportError("Parametros POST no correctos o estan incompletos. Linea: ".__LINE__,$db);

$db = new Db();
$response = array();
$longPag = 50;
$curPag = $longPag * intval($pag);

$db->connect()->autocommit(FALSE);

$stmt = $db->connect()->prepare(
  "/* QUERY HERE */") 
  or reportError("El statement ha fallado. Error en la BD. Linea: ".__LINE__,$db);

$stmt->bind_param("sssssssssii",$idautor,$idautor,$idautor,$cadena,$cadena,$latitud,$latitud,$longitud,$distancia,$curPag, $longPag) 
or reportError("Fallo bindeando los parametros. Linea: ".__LINE__,$db);

$stmt->execute() 
or reportError("No se ha podido ejecutar la consulta. Fallo del servidor. Linea: ".__LINE__,$db);

$stmt->bind_result($idplan,$idautor,$nombre,$descripcion,$privacidad,$modified,$autor,$flagRelacion,$idsitio,$latitud,$longitud,$imagen,$idplan,$idautor,$distancia) 
or reportError("No se han podido asignar valores Linea: ".__LINE__,$db);
$response["planes"]=array();
while($stmt->fetch()){
  $plan=
    array(
      "idplan"=>$idplan,
      "idautor"=>$idautor,
      "nombre"=>$nombre,
      "descripcion"=>$descripcion,
      "privacidad"=>$privacidad,
      "fecha"=>$modified,
      "autor"=>$autor,
      "flagRelacion"=>$flagRelacion,
      "idsitio"=>$idsitio,
      "latitud"=>$latitud,
      "longitud"=>$longitud,
      "imagen"=>$imagen,
      "distancia"=>$distancia
    );
  $response["planes"][]=$plan;
}
$db->connect()->commit() 
or reportError("Fallo en la consignación de la transacción. Linea: ".__LINE__,$db);
$stmt->close();

我不知道为什么但是当我直接在我的数据库中启动它时查询工作正常但是当我尝试使用php时,prepare()函数似乎停止工作。

我试过这个:

$stmt->store_result();
var_dump($stmt->num_rows);  

但结果是NULL。程序没有崩溃(没有调用die)

function reportError ($message,$db){
  $response = array();
  $response["success"]=0;
  $response["message"]=$message;
  return die(print_r(json_encode($response),true));
} 

function checkPostParameters ($requiredParameters){
  $checked = true;
  foreach ($requiredParameters as $parameter){
    if(!array_key_exists($parameter, $_POST) OR is_null($_POST[$parameter])) {
      echo $parameter;
      $checked = false;
    } 
  }
  return $checked;
}

但结果是空的,因为它们不应该根据查询结果。你知道发生了什么吗?

0 个答案:

没有答案