MySQL存储过程PHP的异常行为

问题描述 投票:0回答:1

这是我的MySQL存储过程:

CREATE DEFINER = 'c4_awuser'@'%'
PROCEDURE c4_awdb.aw_spZAAAOttieniDataTables(IN _identificativoTabella VARCHAR(255))
BEGIN

  DECLARE finito INT DEFAULT 0;
  DECLARE nomeColonna VARCHAR(200) DEFAULT '';
  DECLARE titoloColonna VARCHAR(200) DEFAULT '';
  DECLARE queryColonne LONGTEXT DEFAULT 'SELECT ';
  DECLARE _nomeVista VARCHAR(255);
  DECLARE curColonne CURSOR FOR
    SELECT
      COLUMN_NAME,
      COALESCE(nome_campo, COLUMN_NAME) AS nome_campo
    FROM information_schema.columns
      LEFT JOIN aw_tbZAAEGestioneTabelleDati
        ON ZAAEcvVistaAssociata = _nomeVista
      LEFT JOIN aw_tbZAACCampi
        ON nome_campo_db = COLUMN_NAME
        AND tabella = ZAAEcvNomeTabella
    WHERE table_name = _nomeVista;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET finito = 1;

  SELECT
    ZAAEcvVistaAssociata INTO _nomeVista
  FROM aw_tbZAAEGestioneTabelleDati
  WHERE ZAAEcvIdentificativoTabella = _identificativoTabella;

  IF _nomeVista IS NOT NULL AND EXISTS (SELECT
    DISTINCT 1
      FROM information_schema.columns
      WHERE table_name = _nomeVista) THEN

    OPEN curColonne;

    ciclaColonne: LOOP
        FETCH curColonne INTO nomeColonna,titoloColonna;
        IF finito = 1 THEN 
          LEAVE ciclaColonne;
        END IF;

        SET queryColonne = CONCAT(queryColonne,nomeColonna,' AS \'',titoloColonna,'\',');

    END LOOP ciclaColonne;

    CLOSE curColonne;

    IF RIGHT(queryColonne,1) = ',' THEN
        SET queryColonne = LEFT(queryColonne,LENGTH(queryColonne)-1);
    END IF;

    SET queryColonne = CONCAT(queryColonne,' FROM ',_nomeVista);

    SET @sql = queryColonne;
    PREPARE dynamic_statement FROM @sql;
    EXECUTE dynamic_statement;
    DEALLOCATE PREPARE dynamic_statement;

  END IF;

END

有任何错误吗?如果我在MySQL客户端中执行此操作,它会给我2条记录(正确),但是如果我从PHP执行此操作,它会给我0条记录,但没有错误。我将id(例如'elenco_corsi')作为参数传递。我在aw_tbZAAEGestioneTabelleDati中搜索他的视图是什么,然后我使用系统表获取视图的字段,我建立了一个查询并通过语句执行它。


更新

我如何从PHP调用过程。(require DB();定义$conn

require DB();
$id_datatable = "elenco_corsi";
$stmt = $conn->prepare("CALL aw_spZAAAOttieniDataTables(?);");
$stmt->bind_param("s", $id_datatable);
if($stmt->execute()) {
    $result = $stmt->get_result();

    $stmt->close();
    $conn->close();

    echo "<br />Numero righe: ".$result->num_rows."<br /><br />";

    if($result && $result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            echo "yes";
            $dati[] = $row;
        }
        $resp = new stdClass();
        $resp->esito = 1;
        $resp->dati = $dati;
    } else {
        // it enter here
        $resp = new stdClass();
        $resp->esito = 20;
        $resp->dati = $dati;
    }
} else {
    $resp = new stdClass();
    $resp->esito = 30;
    $resp->dati = $dati;
}
mysql stored-procedures dynamic-sql
1个回答
0
投票

游标和准备好的语句不能很好地配合使用。您可以改用MySQLi的query方法:

$table = $conn->real_escape_string($id_datatable);
$sql = "CALL aw_spZAAAOttieniDataTables('$table')";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // Loop through the rows
  while($row = $result->fetch_assoc()) {
    // handle the $row
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.