Create a temporary select file to repeat on other pages

Asked

Viewed 55 times

0

It is possible to do only a select query processing in php and mysql and use the result in pages that are sequels?

Exemplo:
Pagina 1 = faz select de dados, mostra o resultado e reserva.
Pagina 1-1 = mostra o reservado
Pagina 1-2 = mostra o reservado
Pagina 1-3 = mostra o reservado 
  • It is possible with JS

  • Thanks Rafael, you can put an example, pf?

1 answer

1


Following a basic logic, we do the SQL and return a json

// PHP
$retorno = array();
$sql = $pdo->prepare("SELECT * FROM tabela");
$sql->execute();
$retorno['dados'] = $sql->fetchAll();

die(json_encode($retorno));

Done that, by retrieving the json, we would have something similar to that.

[
  {
    "Nome": "Rafael Augusto"
  },
  {
    "Nome": "Geo"
  }
]

Now we could store the result in a sessionStorage to recover on each page.

sessionStorage.setItem('retorno', JSON.stringify(retornoPHP))

Following more or less this logic, you have your return and can access it from any page. Of course there are other ways to do it, but I’m giving this example.

  • Rafael, I will try to include and soon put the result. Thank you !!!

  • I ran the first part and the result is duplicated. That’s how it is?

  • $return = array(); $sql = $Pdo->prepare("select Perg from per WHERE idn2={$N2} AND idrec={$rec} LIMIT 5"); $sql->execute(); $return['data'] = $sql->fetchAll(); die(json_encode($return));

  • I don’t understand, duplicate result?

  • {"dice":[{"Perg":"D u00favida- I made a chocolate liqueur and then..." ,"0":"D u00favida- I made a chocolate liqueur and then....

  • I reduced the text and put only a duplicate.

  • That’s right, you can trade for fetch

  • With fetch showed only 1 record and continues duplicated

  • Can use fetchAll() same, and it’s not duplicated, it’s just a representation of data return. If you watch, the indices are different, it backs from the shape it is in the bank and numbered.

  • OK, I’m going to the 2nd stage. I ask for patience for a new old apprentice (70 years). I will study about sessionStorage. Then put as it was rsrs.

Show 5 more comments

Browser other questions tagged

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