Function that returns mysqli_fetch_array

0

My scenario is as follows: I have a function called UserSearch ()

function buscaUsuario($conexao)
{

$retornar = array();
$query = mysqli_query($conexao, "SELECT user_name FROM usuarios");

while($result = mysqli_fetch_assoc($query))
{
    $retornar[] = $result;
}

return $retornar;

}

I also have a file named demand-user.php and there is the following code snippet:

                        <?php 

                        $all = buscaUsuario($conexao);
                        $ab = sizeof($all);
                        $i = 0;
                        for ($i = 0; $i < $ab; $i++)
                        {
                         ?>
                         <tr>
                            <td><?=implode($all[$i])?></td>
                         </tr> <?php
                         }
                         ?>

I am returning the query in an array to be displayed in a table. Previously I tried to do it in a more succinct way (at least for me) but it would enter an infinite loop and crash the browser. And this other way was as follows: in the userSearch () function I created a variable that received mysqli_fetch_array () and returned that variable. So in search-user.php I would call this function and play within a while so the table would be populated until there was no more row inside the array ... And this (as I said up there) made the browser crash , I decided to do it that way that I showed them. However, if I add another field in the select and I command to print within that, the cells of each row in the table are not populated, just the first row, thus: userNameCompleteSequenceNode

I wanted to understand what I'm doing wrong and what would be the best way to do what I'm trying to do ... Thank you folks!

    
asked by anonymous 28.04.2016 / 21:45

2 answers

1

Dude, you can try doing this:

function buscaUsuario($conexao)
{

$query = mysqli_query($conexao, "SELECT user_name FROM usuarios");

return mysqli_fetch_array($query)

}

And in the other:

<?php 

$all = buscaUsuario($conexao);
while($all)
{
?>
   <tr>
    <td><?php echo $all["user_name"]; ?></td>
   </tr> 
<?php
}
?>

This makes it much more practical and saves you more than half the lines.

    
28.04.2016 / 21:53
0

I was able to list using this way:

function jQueryArray($sql) {
    $tbl = array();
    $linkConexao = conectaBD();
    $rs = mysqli_query($linkConexao, $sql);

    $i = 0;

    while ($tbl = mysqli_fetch_array($rs)){ $i++;

        $tblReturn[$i] = $tbl;

    }


    desconectaBD($linkConexao);
    return $tblReturn;
}

// Lista na view
$tbl = jQueryArray($sql);   

<table  class="wp-list-table widefat fixed striped posts">
    <thead>
            <tr>
                <td>ID</td>
                <td>Motivo</td>
                <td colspan="2"></td>                
            </tr>
        </thead>

        <?php  foreach ($tbl as $value){   ?>

        <tr>
            <td><?php echo $value['id_motivo'];  ?></td>
            <td><?php echo $value['descricao'];  ?></td>
            <td></td>
        </tr>

        <?php  } ?>

    </table>
    
02.08.2018 / 21:11