Page 1 of 1

List of users from MYSQL to page

Posted: Sat Jun 16, 2012 9:08 am
by SquirtLee
Hello everyone,

im having problem with this piece of code. I want it to create list of users registered on my message-system. Order them by username. But im not really sure that im filling the table corectly (actually what it does is create infinite number of rows)


<?php

require_once('inc/commons.php');

$_SESSION['last_page'] = 'search.php';

require_once('inc/top.php');

$message_header_title = 'Seznam uzivatelu';

$method = 'p';
$query_get_users = 'SELECT * FROM  `user` ORDER BY  `username` ASC LIMIT 0 , 30';
$result_get_users = mysql_query($query_get_users);
$user_count = mysql_num_rows($result_get_users);


?>

<div class="content">

<?php require_once('inc/message-header.php'); ?>

    <div class="content-body"><?php
if ($user_count > 0) {
    ?><table class="table-grid1">
                <thead>
                    <tr>
                        <th style="width:20%;">Jmeno</th>
                        <th style="width:20%;">Prijmeni</th>
                        <th style="width:20%;">Uzivatelske jmeno</th>
                        
                        <th>Akce</th>
                    </tr>
                </thead>

                <tbody><?php
   

            while ($row_get_user = mysql_fetch_assoc($result_get_users)) {

        $query_get_users = 'SELECT * FROM  `user` ORDER BY  `username` ASC LIMIT 0 , 30 ';
        $result_get_users = mysql_query($query_get_users);
        $username_encrypted = encrypt($row_get_user['username']);
        $first_name = '';
        $last_name = '';
        $username = '';       
        
        ?><tr> </tr>
                            
                            <td><?php echo $first_name; ?></td>
                            <td><?php echo $last_name; ?></td>
                            <td><?php echo $username; ?></td>

                                                          
        <td><a href="vytvor.php?r=<?php echo encrypt($row_get_users['username']); ?>&m=<?php echo $username_encrypted; ?>" title="Napsat uzivateli zpravu">Napsat zpravu</a>
                           
            <?php
            }
        ?></tbody>
                </table><?php
    }else {
        ?><p>Nepodarilo se zobrazit seznam uzivatelu</p><?php
            }
    ?></div>
    </div>

            <?php require_once('inc/bot.php'); ?>


Re: List of users from MYSQL to page

Posted: Sat Jun 16, 2012 11:45 am
by Temor
You should not be running a query inside the loop. First of all, you're not using the data it fetches, and there is no point in getting the data more than once :)

This should work:
$query_get_users = 'SELECT * FROM  `user` ORDER BY  `username` ASC LIMIT 0 , 30';
$result_get_users = mysql_query($query_get_users);
$user_count = mysql_num_rows($result_get_users);
and it should return 30 rows, but you're saying it returns an infinite amount of rows? Try adding
echo mysql_error();
on the page to see if it gives you an error.

As for this part:
<td><a href="vytvor.php?r=<?php echo encrypt($row_get_users['username']); ?>
you should use $row_get_user['username'], not userS. Or you could just use the $username_encrypted variable, as that will contain the same value.

Re: List of users from MYSQL to page

Posted: Sat Jun 16, 2012 1:03 pm
by jacek
Ideally you would create a function, something like fetch_users() that does the query and returns an array.

Then you can just do
foreach (fetch_users() as $user){
    // do things with $user.
}
It will be much easier to maintain.

Re: List of users from MYSQL to page

Posted: Sat Jun 16, 2012 6:55 pm
by SquirtLee
thank you, everthing works now ;)