Select data and display

2

I'm having trouble querying MySQL in a simple way.

I already have a file in the includes of connection to the bank that works normally. But in the query page I can not display them.

The code I found on Google .

1 - I do not understand the part of loop while . Does it create a variable within the loop itself? (% with%)

2 - What is $clientes ?

Want a simpler way?

    
asked by anonymous 23.02.2015 / 15:53

3 answers

3

There is no simpler form than this.

Yes, it creates a variable when it enters while and reuses this variable every time it passes while by assigning a new value obtained from the database. The function of fetch_object() is just to bring a new row of the database - associated with variable $resultado and the result in case is an object that must be assigned to a variable, in this case the $clientes . If there is no more data to work the fetch_object() returns false indicating that the loop should terminate.

From there inside the loop you can use this variable to access your members that are just the fields that came from the database if you agree with the previously used query .

<?php 
require_once('includes/conexao.php'); //pegou dados da conexão

$sql = 'SELECT * FROM clientes'; //prepara a query a ser mandada para o banco de dados

$resultado = $con->query($sql); //executa a query e guarda o resultado em $resultado
//guarda uma linha do resultado em $clientes (este será um objeto cujos membros serão as 
//colunas da tabela clientes, será false se não tiver novas linhas)
while ($clientes = $resultado->fetch_object()) { //cada passagem aqui pega uma linha nova
    echo $clientes->nome; //pega o membro nome da linha atual que o PHP está avaliando
    echo $clientes->telefone;
} //aqui termina o processamento de uma linha e vai tentar a próxima linha no while
?>
    
23.02.2015 / 15:59
1

One of the advantages of dynamic languages is that it provides more flexible constructs than static languages.

While it works in two parts, first it is checked fetch_object() is different from false, if it is (there is a result of the database) comes the second part to which the result is assigned the variable $clientes , this occurs because of the PHP operator precedence rules .

The assignment operator is one of the least important, so the value of while (true or false) is checked, and if so, the assignment is made.

        2- passo          1 - passo
                     false/ou qualquer coisa
while ($clientes = $resultado->fetch_object()) {
  echo $clientes->nome;
  echo $clientes->telefone;
} 

What can be interpreted as:

while ($clientes = false) //Não entra no while

                           true
while ($clientes = $resultado->fetch_object())// Faz a atribuição
    
23.02.2015 / 16:00
1
  • Whilst assigning return from function " fetch_object " to variable " $ clients "
    ( tip : use "$ client").

  • The "fetch_object" function returns as a object a query result row, so; added to the while, it causes you to access each line of the query result.

  • 25.02.2015 / 20:31