PHPiSean Posted February 16, 2011 Share Posted February 16, 2011 I am a noob at php, looking to become great. My php loop only seems to draw 1 row from the database when listing it out. Here is the loop <?php require('header.php'); mysql_select_db('center'); $sql = "select username, password from users"; $result = mysql_query($sql); $row = mysql_fetch_array($result); $user = $row['username']; $pass = $row['password']; echo "<table border='1'>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>$user</td>"; echo "<td>$pass</td>"; echo "</tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/ Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 There are only 2 records in the table, right? (Ask me how I know that) Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1175290 Share on other sites More sharing options...
gergy008 Posted February 16, 2011 Share Posted February 16, 2011 I am a noob at php, looking to become great. My php loop only seems to draw 1 row from the database when listing it out. Here is the loop <?php require('header.php'); mysql_select_db('center'); $sql = "select username, password from users"; $result = mysql_query($sql); $row = mysql_fetch_array($result); $user = $row['username']; $pass = $row['password']; echo "<table border='1'>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>$user</td>"; echo "<td>$pass</td>"; echo "</tr>"; } echo "</table>"; ?> I don't know if this will help but your setting variables before you cave made the array with mysql_fetch_array. Try using this: <?php require('header.php'); mysql_select_db('center'); $sql = "select username, password from users"; $result = mysql_query($sql); echo "<table border='1'>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>$row['username']</td>"; echo "<td>$pass['password']</td>"; //Remeber this is a loop so every time it passes through each row it gets a new username and password, Opposed to you echoing the first line before. echo "</tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1175293 Share on other sites More sharing options...
petroz Posted February 16, 2011 Share Posted February 16, 2011 There are only 2 records in the table, right? (Ask me how I know that) LOL!!! How do you know??? Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1175297 Share on other sites More sharing options...
PHPiSean Posted February 16, 2011 Author Share Posted February 16, 2011 There are only 2 records in the table, right? (Ask me how I know that) Umm... how? Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1175300 Share on other sites More sharing options...
PHPiSean Posted February 16, 2011 Author Share Posted February 16, 2011 There are only 2 records in the table, right? (Ask me how I know that) Because I'm always posting about problems? Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1175303 Share on other sites More sharing options...
Pikachu2000 Posted February 16, 2011 Share Posted February 16, 2011 There are only 2 records in the table, right? (Ask me how I know that) Because I'm always posting about problems? LOL, no nothing like that. That code will always echo one less record than it returns, and it will always echo only the first record. See my comments in the code . . . $sql = "select username, password from users"; $result = mysql_query($sql); $row = mysql_fetch_array($result); // <--- This takes the first record and moves the data pointer to the second record $user = $row['username']; // <--- Move inside the while() loop $pass = $row['password']; // <--- Move inside the while() loop echo "<table border='1'>"; while($row = mysql_fetch_array($result)) { // <--- When you call mysql_fetch_array() again, the data pointer is already pointing to the second record echo "<tr>"; echo "<td>$user</td>"; // <--- echos the value from the first record, assigned above echo "<td>$pass</td>"; // <--- echos the value from the first record, assigned above echo "</tr>"; } echo "</table>"; Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1175309 Share on other sites More sharing options...
PHPiSean Posted February 16, 2011 Author Share Posted February 16, 2011 There are only 2 records in the table, right? (Ask me how I know that) Because I'm always posting about problems? LOL, no nothing like that. That code will always echo one less record than it returns, and it will always echo only the first record. See my comments in the code . . . $sql = "select username, password from users"; $result = mysql_query($sql); $row = mysql_fetch_array($result); // <--- This takes the first record and moves the data pointer to the second record $user = $row['username']; // <--- Move inside the while() loop $pass = $row['password']; // <--- Move inside the while() loop echo "<table border='1'>"; while($row = mysql_fetch_array($result)) { // <--- When you call mysql_fetch_array() again, the data pointer is already pointing to the second record echo "<tr>"; echo "<td>$user</td>"; // <--- echos the value from the first record, assigned above echo "<td>$pass</td>"; // <--- echos the value from the first record, assigned above echo "</tr>"; } echo "</table>"; Thanks, problem solved Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1175312 Share on other sites More sharing options...
petroz Posted February 16, 2011 Share Posted February 16, 2011 Here is what your looking for... <?php require('header.php'); mysql_select_db('center'); $sql = "select username, password from users"; $result = mysql_query($sql); //$row = mysql_fetch_array($result); //$user = $row['username']; //$pass = $row['password']; echo "<table border='1'>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>$row['username']</td>"; echo "<td>$row['password']</td>"; echo "</tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1175313 Share on other sites More sharing options...
undertaker333 Posted August 25, 2011 Share Posted August 25, 2011 i wasted alot of time on it but i dont know wat i m missing.... loop is giving me only one output <?php $comments= ""; $sql= "SELECT * FROM comment"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { $serials= $row['serial']; $names= $row['name']; $emails= $row['email']; $msges= $row['msg']; $date_added= $row['date']; $comments= "$names <br /> $msges <br /> $date_added"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1261999 Share on other sites More sharing options...
Psycho Posted August 25, 2011 Share Posted August 25, 2011 i wasted alot of time on it but i dont know wat i m missing.... loop is giving me only one output <?php $comments= ""; $sql= "SELECT * FROM comment"; $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { $serials= $row['serial']; $names= $row['name']; $emails= $row['email']; $msges= $row['msg']; $date_added= $row['date']; $comments= "$names <br /> $msges <br /> $date_added"; } ?> Why are you posting in this thread that is 6 months old? Post a new thread. Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1262003 Share on other sites More sharing options...
jcbones Posted August 25, 2011 Share Posted August 25, 2011 When in doubt, copy/paste the query string into phpMyAdmin, or equal database software. You could also type it into the mysql console. Quote Link to comment https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/#findComment-1262004 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.