How to execute a procedure in PHP

3

I'm trying to create a listing and I'm having trouble calling the select procedure.

Code connecting to the bank:

<?php 
 $conexao = mysql_connect("localhost", "admin", "admin") or print (mysql_error()); 
 print "Conexão OK!"; 
 mysql_close($conexao); 
 mysql_select_db("des_arcamel", $conexao) or print(mysql_error()); 

?>

PHP code

<!DOCTYPE html>

 <?php
 REQUIRE_ONCE "conexao.php";
 REQUIRE_ONCE "funcoes.php";


 echo $result = mysqli_query($conexao, "CALL sp_se_lista") or die("Query fail: " . mysqli_error()); die;
 ?>

<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
   <title>Estoque</title>

<!-- Bootstrap core CSS -->
<link href="boostrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap theme -->
<link href="boostrap/css/bootstrap-theme.min.css" rel="stylesheet">

<div class="container">  
<table class="table table-striped">
<tr>
<thead>
  <tr>
    <th>Código Produto</th>
    <th>Descrição</th>
    <th>Preço</th>
    <th>Quantidade Estoque</th>
  </tr>
 </thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)){   


<td><?php echo $row[cod_produto] ?></td>
<td><?php echo $row[dsc_produto] ?></td>
<td><?php echo $row[preco_produto] ?></td>
<td><?php echo $row[qtd_estoque] ?></td>

 </tr>

 }
 </tbody>
 </table>

And the procedure in the bank

DELIMITER $$

USE 'des_arcamel'$$

DROP PROCEDURE IF EXISTS 'sp_se_lista'$$

CREATE DEFINER='admin'@'localhost' PROCEDURE 'sp_se_lista'()
 BEGIN

SELECT 
cod_produto,
dsc_produto,
preco_produto,
qtd_estoque
FROM
estoque;

END$$

 DELIMITER ;

Can anyone help?

Editing:

The problem is the time to pull the procedure, I edited the code and now I have this:

<?php
REQUIRE_ONCE "conexao.php";
REQUIRE_ONCE "funcoes.php";
$result = "CALL sp_se_lista()";
$rstSql = executeStoredProcedure($result);
while ($rows = mysqli_fetch_assoc($rstSql)){   

echo $rows[cod_produto]; ?>

and the function is:

<?php

require_once ("conexao.php");

function executeStoredProcedure($strSql) {
global $conexao;
$rstExe = mysqli_query($conexao, $strSql);

}

When I give echo in REQUIRE tb it does not show anything ...

    
asked by anonymous 10.09.2015 / 03:31

1 answer

1

As you can see, you are mixing mysql_* on the connection and calling the query with mysqli_* . The method you need to use is basically this:

        //conecta no banco
          $connection = mysqli_connect("localhost", "admin", "admin", "des_arcamel", "3306"); //ou 3307

          //executa o store procedure
          $result = mysqli_query($connection, 
             "CALL sp_se_lista();") or die("Erro na query da procedure: " . mysqli_error());

          //lista os resultados
          while ($row = mysqli_fetch_array($result)) {   
              echo  $row[0] . "<br>\n" .
                       $row[1] . "<br>\n"; 
          }
    
11.09.2015 / 06:01