How to run a PHP database

Asked

Viewed 7,226 times

4

I’m trying to create a listing and I’m having trouble calling the Select Database.

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 bank trial

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 ;

Someone can help?


Editing:

The problem is time to pull the file, 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 echo in REQUIRE tb shows nothing...

  • Which error will return?

  • mysql_* functions do not work properly with sp

  • Shows no error. I am using Notepad++. I must use another?

  • You are using mysqli_* and mysql_* together, it will never work.

1 answer

2


From what I can tell you’re mixing mysql_* on connection and calling 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"; 
          }
  • I believe that was it, thank you very much Ivan!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.