Store all records in a table in a variable (PHP)

1

I need to query my MySQL database and store all rows that are returned within the $dadosBrutos variable. The purpose is for each line to be separated by the character "§", for example: nomeCliente1,telefoneCliente1§nomeCliente2,telefoneCliente2§nomeCliente3,telefoneCliente3 and so on.

The data will be obtained with the SELECT * FROM tbClientes command, but I have no idea how to separate one line from the other and store them within the $dadosBrutos variable.

I tried to make this code with Do...While , but I have no idea how to continue.

For now I have the following code:

if($qtdeClientesCadastrados > 0) {  
    $contador = 0; 
    do {

    } while ($contador < $qtdeClientesCadastrados);
} else { 
    echo "zero_clientes_cadastrados";
}

Notes:

  • The variable $qtdeClientesCadastrados has already been declared in the snippet above the posted code and stores the number of clients that are registered in tbClientes of the database;

  • The entire database connection system is already up and running correctly;

  • If there is a better method to make code without being Do...While , it is also useful, since at the end it is possible to give the command echo $dadosBrutos; .

asked by anonymous 27.12.2018 / 02:32

2 answers

3
  • Assuming the column names are cliente and telefone
  • For the § character in all cases below, I used header("Content-type: text/html; charset=windows-1252");

Functional sample

Functional sample table.

dataforconnection.php

$hostname="localhost";  
    $username="USUARIO";  
    $password="SENHA";  
    $db = "Nome_DB";

Using PDO

$dbcon = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);

$sql = "select cliente,telefone from tbClientes";
$stmt = $dbcon->prepare($sql);
$stmt->execute();

if ($data = $stmt->fetch()) {
    do {
        $dadosBrutos .= $data['cliente'].','.$data['telefone'].'§'; 

    } while ($data = $stmt->fetch());

    $dadosBrutos= substr($dadosBrutos,0,-1); //AQUI ELIMINA O ULTIMO §

} else {
    $dadosBrutos= "zero_clientes_cadastrados";
}

echo $dadosBrutos;

Using mysqli

$dbcon = new mysqli($hostname, $username, $password, $db);

$query = $dbcon->query("select cliente,telefone from tbClientes");

$qtdeClientesCadastrados=mysqli_num_rows($query);
  

option 1

if($qtdeClientesCadastrados>0){

    do {
        $dadosBrutos .= $data['cliente'].','.$data['telefone'].'§'; 

    } while ($data = $query->fetch_array()); 

      $dadosBrutos=substr($dadosBrutos, 2); // elimina os dois primeiros caracteres (,§)
     $dadosBrutos= substr($dadosBrutos,0,-1); //AQUI ELIMINA O ULTIMO §

}else{
     $dadosBrutos= "zero_clientes_cadastrados";
}

echo $dadosBrutos;
  

option 2

if($qtdeClientesCadastrados>0){

    while($row = $query->fetch_array())
    {       
        $dadosBrutos .= $row['cliente'].','.$row['telefone'].'§'; 
    } 

     $dadosBrutos= substr($dadosBrutos,0,-1); //AQUI ELIMINA O ULTIMO §

}else{
     $dadosBrutos= "zero_clientes_cadastrados";
}

echo $dadosBrutos;
    
27.12.2018 / 08:27
0

Objective: echo $dadosBrutos;

Solution made with implode () in arrays resulting from the query.

I tried to simplify the code as much as I could.

If you did not want to use $ result as a function parameter, slightly change the getDatabase function and use mysql_fetch_array

<?php
    //Conexão apenas para exemplo
    $conn = new mysqli($servername, $username, $password, $dbname);
    if($conn->connect_error) die("Connection failed: " . $conn->connect_error);
    $query = $conn->query("SELECT nomeCliente, telefoneCliente FROM tbClientes");
    $conn->close();

    //Joga a query na função para obter os dados concatenados
    $dadosBrutos = getDadosBrutos($query);

    //objetivo
    echo $dadosBrutos;

    function getDadosBrutos($result){
        $clientes = array();
        if($result->num_rows > 0){
            while($row = $result->fetch_assoc()){
                $clientes[] = implode(',',$row);
            }
        }else{
            //nenhum cliente
            return '';
        }
        return implode('§', $clientes);
    }
?>
    
27.12.2018 / 16:25