Select data and display

Asked

Viewed 780 times

2

I’m having trouble looking up Mysql in a simple way.

I already have a file on includes connection to the bank that works normally. But I’m not showing them.

The code I found on Google.

1 - I don’t understand the part of loop while. It creates a variable within itself loop? ($clientes)

2 - What is fetch_object()?

I wanted a simpler medium?

  • Since you’re returning to school now... I advise you to study PDO. :)

  • The steps to perform a query in the database are: 1 - turn the sql string into a query. 2 - Pass this 'result' to the function/method that extracts the bank lines, it is usually necessary to use a for/while to traverse all lines.

  • Where can I find this material for PDO studies? Do you know anywhere? ?

  • 1

    @Rafaelacioly you don’t need to do this, PDO won’t help you at all. Check the answers, and let us know if there’s anything missing to understand better.

  • @Rafaelacioly no conselhei PDO por ajudar em sua dúvida, I advised because it is cleaner and supports several databases; http://code.tutsplus.com/pt/tutorials/pdo-vs-mysqliwhich-should-you-use--net-24059 | http://stackoverflow.com/questions/2190737/what-is-Difference-between-mysql-mysqli-and-Pdo

  • @Rafaelacioly You can accept an answer if it solved your problem. You can vote on all the posts on the site as well. Did any help you more? Something needs to be improved?

Show 1 more comment

3 answers

3

There’s no simpler form than that.

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

From within the loop you can use this variable to access its members which are precisely the fields that came from the database according to the query previously used.

<?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
?>

I put in the Github for future reference.

1

  1. while is assigning function return "fetch_object" the variable "$customers"
    (tip: use "$customer").

  2. The "fetch_object" function returns as object a query result line, therefore; added to while, it causes you to access each query result line.

1

One of the advantages of dynamic languages is that it provides constructions more flexible than the languages of Statics.

while it works in two parts, first it checks fetch_object() is different from false, if (there is a result of the database) is the second part to which the result assignment is made the variable $clientes, this occurs due to the rules of precedence of operators of PHP.

The assignment operator is one of the least senior, so the value of while(true or false) is checked and in the positive case the assignment is made.

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

Which can be interpreted as:

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

                           true
while ($clientes = $resultado->fetch_object())// Faz a atribuição

Browser other questions tagged

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