Sven
Sven

Reputation: 43

User name output as a list

I want to show all my users in a list( or table ) but the way I did doesn't work. The list is empty. I appreciate every help I can get!

<?php
 include_once('connect.php');

 $pdostatement = $conn->prepare('SELECT f_name FROM tbl_user ' );
 $pdostatement->execute();

 $list =  $pdostatement->fetchAll();

 $value_fname = $list;

?>

<ul>
  <li value="<?=$value_fname ?>"></li>
</ul> 

Here is a part of my database that will be needed for my userlist. If you need the complete database, I will immediately edit my post!

id  int(11) AUTO_INCREMENT
state   varchar(255)
f_name  varchar(255)

EDIT : Thanks all of you for the fast answer! It did work! Wish ya a nice day!

EDIT : I can't accept more then 1 answer so I choosed van Stein en Groentjes because he was a bit earlier. But there were more then one correct answer! Thanks!

Upvotes: 2

Views: 52

Answers (3)

I wrestled a bear once.
I wrestled a bear once.

Reputation: 23379

If you're expecting more than one row in the result loop them like the others suggested, if you're only expecting a single row, use this.

 $list =  $pdostatement->fetch();
 $value_fname = $list['f_name'];

Upvotes: 2

Peter Kota
Peter Kota

Reputation: 8350

try:

<ul>
<?php 
foreach($value_fname as $key => $fname) {
    echo '<li value="' . $fname . '"></li>'
}
?>
</ul>

Upvotes: 2

van Stein en Groentjes
van Stein en Groentjes

Reputation: 233

You need to loop over your result set

<ul>
<?php
foreach($value_fname as $row) {
    echo '<li value="'.$row['f_name'].'"></li>';
}
?>
</ul>

Upvotes: 3

Related Questions