Jump to content

Recommended Posts

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>";

?>

Link to comment
https://forums.phpfreaks.com/topic/227921-mysql-while-loop-not-working/
Share on other sites

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>";

?>

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>";

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 :)

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>";

?>

 

  • 6 months later...

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";

}

 

?>

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.