Jump to content

WHILE with php and mysql


magcr23

Recommended Posts

Hi guys, i have an table with that:

 

id         id_user        mensagem

1             1                  bom

2             2                  mau

3             3                  +/-

 

and i'm trying to show all data (and keep doing if exist more data). That's my code:

<?php
while($e = mysqli_fetch_assoc($a)){
?>
<div class="col-sm-3">
<blockquote>
<p class="clients-words"> 
<?php
echo $e['id']; 
echo '<br />';
echo $e['mensagem']; 
?>
</p>
<span class="clients-name text-primary laranja">
<?php
echo '-' . $d['nome'];
?>
</span>
<img class="img-circle img-thumbnail" src="http://lorempixel.com/400/400/people/1/">
</blockquote>
</div>
<?php 
}
?>

But with that, it don't show the first one and this:

echo '-' . $d['nome'];

always echo the same, the first data of the database "cliente".

how can i fix that?

Link to comment
https://forums.phpfreaks.com/topic/297117-while-with-php-and-mysql/
Share on other sites

it don't show the first one

 

 

^^^ you are probably fetching one row before the start of your loop or you sql query is wrong.

 

always echo the same, the first data of the database "cliente"

 

 

^^^ the code you showed us is fetching the database rows into $e, not $d. $d must be some other variable from before the start of this code. if you are running the query/code you posted in this thread inside of a loop, you shouldn't. you should form and run ONE JOINed query that gets all the related data at one time.

It's really best to use real world variable names that make sense and are meaningful so you don't confuse yourself, or someone else who has to work on the code. $a, $b, $c? That makes you have to sit there and decipher everything which wastes time because it's not immediately clear what's going on. It should be easily readable by a human. PHP doesn't care how long your variable names are and it doesn't affect speed or processing, so why not use real words as variable names and save yourself some time and confusion?

while($row = mysqli_fetch_assoc($query)){
  //...
  echo $row['id'];
  echo '<br />';
  echo $row['mensagem'];
  //...
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.