magcr23 Posted June 30, 2015 Share Posted June 30, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/297117-while-with-php-and-mysql/ Share on other sites More sharing options...
mac_gyver Posted June 30, 2015 Share Posted June 30, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/297117-while-with-php-and-mysql/#findComment-1515275 Share on other sites More sharing options...
Solution CroNiX Posted June 30, 2015 Solution Share Posted June 30, 2015 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']; //... } Quote Link to comment https://forums.phpfreaks.com/topic/297117-while-with-php-and-mysql/#findComment-1515291 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.