How to use a fetch_array contained in another PHP page?

0

Example: SELEC It has three names, I want to print these 3 names in another page.

pagina1.php

<?php
session_start();
include_once("config.php");
?>

<?php 
$sql = $db->query("SELECT nome FROM banco_de_dados");//SELECT TEM 3 nomes (vitor,matheus,carol)
?>

<?php
    while($dados = $sql->fetch_array()){
    $nome = $dados['nome'];

    $_SESSION['nome'] = $nome;
    };

?>

page2.php

<?php
session_start();
$nome = $_SESSION['nome'];
echo $nome;
?>

print on page:

vitor
matheus
carol
    
asked by anonymous 29.06.2018 / 00:00

3 answers

0

page1.php

while($dados = $sql->fetch_array()){
//concatena os nomes separando-os por virgula
$nomes .= $dados['nome'].",";
};

//retira a ultima virgula
$nomes=substr($nomes, 0, -1);
$_SESSION['nome'] = $nomes; / vitor,matheus,carol

page2.php

If you just print each name on one line, it does replace the comma by line break

session_start();
$nome = $_SESSION['nome'];

echo str_replace(',','<br>',$nome);

or

session_start();

$nomes = explode(',',$_SESSION['nome']);

echo $nomes[0];
echo "<br>";
echo $nomes[1];
echo "<br>";
echo $nomes[2];
    
29.06.2018 / 02:39
1

Your question is a bit confused, but I think I get it. In case you are only printing a name because the name is being overwritten in $ _ SESSION (I already had this problem in my TCC). What can be done to solve this problem is instead of using $ _ SESSION you can call the direct page, like this:

<?php
session_start();
include_once("config.php");

$sql = $db->query("SELECT nome FROM banco_de_dados");//SELECT TEM 3 nomes (vitor,matheus,carol)

while($dados = $sql->fetch_array()){
echo $dados['nome'];
}

Already on the page where it will be shown:

<?php
session_start();
require_once 'pagina1.php';

So it will run page1 on page2, showing the data inside page2.

    
29.06.2018 / 00:07
0

Do this:

$num = 0;
while($dados = $sql->fetch_array()){
$nome = $dados['nome'];
$_SESSION['nome_id'.$num] = $nome;
$num++;
}

On page 2 you put the sequence:

echo $_SESSION["nome_id1"];
echo $_SESSION["nome_id2"];
echo $_SESSION["nome_id3"];

Or it does as the member of the first response.

    
29.06.2018 / 00:11